在这个时钟中,我最初要求用户输入时间,但它不会返回变量中的时间。我得到一个
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
我知道设置时间部分不需要成为一个函数,但我想稍后使用它。
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()