我想要一个接受自定义Inpu的倒计时

2024-04-20 02:01:50 发布

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

所以我尽了最大的努力让我的代码正常工作我想要的是一个输入响应倒计时有人能帮我吗?你知道吗

    print ("Would You like To Start A Countdown? Y/N (CASE SENSITIVE)")
countdownyn = input (':')
if countdownyn == ('Y'):
    print ("Please Enter Your Designated Time To Countdown From")
x = input (':')
def countdown(x) :
    while x> 0:
        print (x)
        print ("")
        time.sleep(1)
        x = x1
    if x ==0:
        print("BLAST OFF!")
countdown(x)

Tags: to代码youinputifstartlikecase
2条回答

我将不列出几个问题,但这里有一些解决方法。也许 吧。如果您使用的是python3,请将原始输入更改为input。你知道吗

import time

def countdown(x) :
    while x > 0:
        print (x)
        print ("")
        time.sleep(1)
        x = x - 1
    print("BLAST OFF!")

countdownyn = raw_input ('Would You like To Start A Countdown? Y/N (CASE SENSITIVE): ')
if countdownyn == ('Y'):
    x = raw_input ('Please Enter Your Designated Time To Countdown From: ')
    countdown(int(x))

带所需暂停的输出:

Would You like To Start A Countdown? Y/N (CASE SENSITIVE): Y
Please Enter Your Designated Time To Countdown From: 10
10

9

8

7

6

5

4

3

2

1

BLAST OFF!

****编辑****

为了让你走得更远……:

import time

def countdown(ticks) :
    for tick in range(ticks, 0, -1):
        print (tick)
        print ("")
        time.sleep(1)
    print("BLAST OFF!")



while True:
    countdownyn = raw_input('Would You like To Start A Countdown? Y/N (CASE SENSITIVE): ')
    if countdownyn == 'Y':
        x = raw_input('Please Enter Your Designated Time To Countdown From: ')
        try:
            countdown(int(x))
        except ValueError:
            print("Please enter a valid integer")
            continue
    elif countdownyn == 'N':
        print("Goodbye!")
        break
    else:
        print("Please enter only Y or N")

你从不对x1做任何事。也许你的意思是x-1

while x> 0:
    print (x)
    print ("")
    time.sleep(1)
    x = x-1 #here 

相关问题 更多 >