Python,我如何制作一个倒计时时钟来询问用户想要的数字是多少

2024-03-28 20:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,所以我对python是新手,我真的需要一些帮助。这是我目前的代码。我不断地得到一个语法错误,我不知道我做错了什么

count = int(input("What number do you want the timer to start: "))
count == ">" -1:
print("count")
print("")
count = count - 1
time.sleep(1)

Tags: theto代码younumberinputcountdo
3条回答

语法错误可能来自

count == ">" -1:

我不知道你从哪儿弄来的!您需要的是一个循环,当计数器用完时停止,否则重复相同的代码。你知道吗

count = int(input("What number do you want the timer to start: "))
while count > 0:
    print("count", count)
    print("")
    count = count - 1
    time.sleep(1)

也可以用count -= 1替换count = count -1,但这不会对代码的操作产生任何影响。你知道吗

您需要先确保导入时间库,然后才能访问时间。睡眠方法。你知道吗

另外,使用循环来重复代码可能更有效。if语句的结构也不正确,不是正确的表达式。你知道吗

IF <Expression> is TRUE:
    DO THIS.

还可以考虑在for循环中使用一个范围,如下所示

import time
count = int(input("What number do you want the timer to start: "))
for countdown in range(count,0,-1):
    print (countdown)
    time.sleep(1)

解释

for countdown in range(count,0,-1): 

范围(起点、终点、步长)。从给定的整数开始,到0结束,每次迭代步进-1。你知道吗

在第二行中,不能从字符串“>;”中减去1。 你需要的显然是一个for循环。编辑:你也忘了导入!你知道吗

import time
count = int(input("What number do you want the timer to start: "))
for i in range(count):
    print("count")
    print(i)
    count = count - 1
    time.sleep(1)

相关问题 更多 >