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

时间:2017-06-14 22:42:36

标签: python time

所以我已经尝试过我的绝对最难尝试让我的代码工作我想要的是输入响应倒计时可以有人帮忙吗?

    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)

2 个答案:

答案 0 :(得分:0)

你永远不会对x1做任何事情。也许你的意思是x-1

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

答案 1 :(得分:0)

我不会列出几个问题,但这里有一些帮助。也许。如果您使用的是Python 3,请将raw_input更改为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!

**** **** EDIT

为了让你更进一步......:

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")