0

In this clock i made it initially asks for the user to input the time but when it wont return the time in variables. I get a

Traceback (most recent call last):
  File "C:/Documents and Settings/Brett/My Documents/Programming/Python/clock.py", line 20, in <module>
    print(hour,":",mins,":",sec)
NameError: name 'hour' is not defined

I know the set time part dosnt need to be a function but i want to use it later.

import time

def setTime():
    print("Set time \n--------")
    time=input("24 hour or 12 hour \nType 1 or 2: ")
    if time=="2": hour12=input("AM or PM \nType 1 or 2: ")
    else: hour12=0
    hour=input("Hour: ")
    mins=input("Minute: ")
    print("--------")
    sec=0
    return time,hour12,hour,mins,sec

def printTime24():
    print(hour,":",mins,":",sec)

#def printTime12():

setTime()
print(hour,":",mins,":",sec)
while True:
    time.sleep(1)
    sec+=1
    if sec == 60:
        sec=0
        mins+=1
    if mins == 60:
        mins=0
        hour+=1
    if time=="1": printTime24()
#    elif time=="2": printTime12()

1 Answer 1

3

The return statement returns values, not variables. And if nothing is done with the returned values, they are discarded.

You call setTime() but do nothing with the values it returns.

You need to store the returned values in new names:

time, hour12, hour, mins, sec = setTime()

I used the same names as used in the setTime() function itself, but I could just as well have given them different names:

foo, bar, baz, spam, eggs = setTime()

and then used those names in the rest of the code:

print(baz, ":", spam, ":", eggs)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.