Member Avatar for powerade661

So I am trying to make this code run in a constant loop. Any ideas how I would do this? As I said before I am trying to self teach myself Python and so far I have been successful.

import os
import datetime
import time
print('What would you like to do?')

answer = raw_input()
today = datetime.date.today()

if answer == 'shutdown':
        print('This works')

elif answer == 'restart':
       print('This also does work')

elif answer == 'restart':
        print('This also works fine')

elif answer == 'date':
        print today

elif answer == 'time':
        print (time.strftime("%H:%M:%S"))

else:
   print('Did not recognize that request, sorry.')

.... in a constant loop ...

Do you mean you want to put all of those code in an infinite loop? You can use while True:. First, you indent all of your code print('What would you like to do?') and below. Then add the while loop above it. So it should look like this

 while True:
    print('What would you like to do?')
    ## --- all the code below the above code

Indentation is one of the thing that I hate the most in Python. It usually force you to care about this little detail. Usually, it is terrible bad because you need to be consistence with your indentation (use 2 space? 3 space? or tab?). By the way, enjoy your journey to mastering Python ^^

Member Avatar for powerade661

I want it to go switch back to "what would you like to do?" After it has finished whatever the input was. Sorry for being so vague.

So basically, you want to ask "what would you like to do?". When user has already input what they want to do, it will re-ask "what would you like to do?" again and the process keep repeat infinitely? if this is what you want, my above answer fits it very well.

I think it is clearer if I provide the full code

import os
import datetime
import time

while True:
    print('What would you like to do?')

    answer = raw_input()
    today = datetime.date.today()

    if answer == 'shutdown':
        print('This works')
    elif answer == 'restart':
        print('This also does work')
    elif answer == 'restart':
        print('This also works fine')
    elif answer == 'date':
        print today
    elif answer == 'time':
        print (time.strftime("%H:%M:%S"))
    else:
        print('Did not recognize that request, sorry.')
Member Avatar for powerade661

Yes that is what I want. But I still want the input to show what the user puts, before it goes back to the "what would you like to do."

Member Avatar for powerade661

This is what I am confused about. How would I make it so that it doesn't display the extra zero in there. So that it just says 4:00 AM and still displays the 11:00 AM whenever it is that time. Also this may sound strange, but is it possible to clear the screen with the other results that was inputted when it loops back around? Thanks again for your help.

but is it possible to clear the screen with the other results that was inputted when it loops back around?

Yes and no. If you are running from Windows terminal, you can use os.system('cls') if you are using Linux, you can use os.system('clear').

So that it just says 4:00 AM and still displays the 11:00 AM

Change time.strftime("%H:%M:%S") to str(time.localtime().tm_hour) + time.strftime(":%M:%S")

Member Avatar for powerade661

Sorry for the misunderstanding. I meant take it from being 07:00 AM to being just 7:00 AM but it still says 11:00 AM if that is the current time. Also would I put the os.system(cls) under the while True: ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.