与学徒招聘代理人联系后,我被赋予任务去给我的时钟编程.我目前有一个工作时钟,但是我仍然不满意,因为必须在程序中实现当前时间才能使其成为可用时钟,而我希望用户能够这样做以使其更加用户友好.
我尝试删除预设了小时,分钟和秒的位置,将其替换为以下内容:
hours = input("Set the amount of hours\n")
minutes = input("Set the amount of minutes\n")
seconds = input("Set the amount of seconds\n")
但是,这会产生错误:
‘TypeError:无法将’int’对象隐式转换为str’
它将按计划的预设时间打开时钟,但是从该时间开始不开始计时.
hours = input("Set the amount of hours\n")
minutes = input("Set the amount of minutes\n")
seconds = input("Set the amount of seconds\n")
#hours=15
#minutes=5
#seconds=0
import time
from turtle import*
setup()
t1 = Turtle()
while True:
t1.clear()
t1.write(str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":"
+ str(seconds).zfill(2),font=("arial",60,"bold"))
seconds = seconds+1
time.sleep(1)
if seconds == 60:
seconds = 0
minutes = minutes+1
if minutes == 60:
minutes =0
hours = hours+1
if hours ==24:
seconds=0
minutes=0
hours=0
通常,我希望乌龟程序根据用户输入的时间来打开显示时间,但是确实会崩溃,并且无法像预期的24小时制那样工作.
最佳答案
键盘上的用户输入为字符串类型.由于要对它们执行算术运算,因此应将它们转换为整数类型,如下所示.其余代码看起来还可以.
hours = int(input("Set the amount of hours\n"))
minutes = int(input("Set the amount of minutes\n"))
seconds = int(input("Set the amount of seconds\n"))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。