Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If statments are ignored by code after an input in Python?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading