加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python编程时钟时,如何允许用户输入当前时间的自己的值?

发布时间:2020-12-17 17:36:24 所属栏目:Python 来源:网络整理
导读:与学徒招聘代理人联系后,我被赋予任务去给我的时钟编程.我目前有一个工作时钟,但是我仍然不满意,因为必须在程序中实现当前时间才能使其成为可用时钟,而我希望用户能够这样做以使其更加用户友好. 我尝试删除预设了小时,分钟和秒的位置,将其替换为以下内容: h

与学徒招聘代理人联系后,我被赋予任务去给我的时钟编程.我目前有一个工作时钟,但是我仍然不满意,因为必须在程序中实现当前时间才能使其成为可用时钟,而我希望用户能够这样做以使其更加用户友好.

我尝试删除预设了小时,分钟和秒的位置,将其替换为以下内容:

hours = input("Set the amount of hoursn")
minutes = input("Set the amount of minutesn")
seconds = input("Set the amount of secondsn")

但是,这会产生错误:
????‘TypeError:无法将’int’对象隐式转换为str’

它将按计划的预设时间打开时钟,但是从该时间开始不开始计时.

hours = input("Set the amount of hoursn")
minutes = input("Set the amount of minutesn")
seconds = input("Set the amount of secondsn")

#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 hoursn"))
minutes = int(input("Set the amount of minutesn"))
seconds = int(input("Set the amount of secondsn"))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读