Ad

Invalid Token And If Statement Not Working

This is my first time programming in Python and I am not too sure what I am doing wrong. I am simply trying to print what the hour (time) is in words however I get the error "SyntaxError: invalid token" when I set the if statement to check

if current_hour == 05:

and when I change the 05 to 5 the If Statement simply does nothing.

This is my code:

import time

current_hour = time.strftime("%I")
print(current_hour)

if current_hour == 05:
    print("Five")

Thank you!

Ad

Answer

current_hour is a string. For this to work you need the following:

import time

current_hour = time.strftime("%I")
print(current_hour)

if current_hour == '05':
    print("Five")

or this:

import time

current_hour = int(time.strftime("%I"))
print(current_hour)

if current_hour == 5:
    print("Five")
Ad
source: stackoverflow.com
Ad