与学徒招聘代理人联系后,我被赋予任务去给我的时钟编程.我目前有一个工作时钟,但是我仍然不满意,因为必须在程序中实现当前时间才能使其成为可用时钟,而我希望用户能够这样做以使其更加用户友好.
我尝试删除预设了小时,分钟和秒的位置,将其替换为以下内容:
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 timefrom 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"))
总结 以上是内存溢出为你收集整理的使用python编程时钟时,如何允许用户输入当前时间的自己的值? 全部内容,希望文章能够帮你解决使用python编程时钟时,如何允许用户输入当前时间的自己的值? 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)