I am new to Python and am learning about the time
module. I was messing around with the module trying to write a program that outputs either UTC or Local time depending on the user’s input, however when I run the program newtime = input("Would you like to see Local or UTC time? ")
executes, but not the if statements.
import time
newtime = input("Would you like to see Local or UTC time? ")
#newtime stores user input
if newtime.lower == "utc":
#If newtime lowercase equals to utc this if line will run
print(time.strftime("The current time and date in UTC is: %c ", time.gmtime()))
#strftime formats the struct_time from gmtime() into something more readable
elif newtime.lower == "local":
#If newtime lowercase equals to local this elif line will run
print(time.strftime("The current time and date in local is: %c", time.localtime()))
#strftime formats the struct_time from localtime() into something more readable
else:
print("Input not recognized, please type 'Local' or 'UTC'")
#If neither local or utc are inputed this prints
I thought the problem might be newtime
but that wasn’t it. I originally had newtime = input("Would you like to see Local or UTC time? ").lower
because I’m new to programming but that wasn’t it either.
>Solution :
You need to call the functions in your if statements:
newtime = input("Would you like to see Local or UTC time? ")
if newtime.lower() == "utc":
...
elif newtime.lower() == "local":
...
else:
...
Otherwise you are just doing:
if str.lower == 'utc':
...
Which will always be False
because a function is never equal to a string