如何倒数,永远数到最后

2024-06-01 05:53:23 发布

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

i = 10
while i>=1:
    print("count down in " + str(i))
    i = i - 1 #or i +=1

if i<10:
    print("blasting off")

    if i==0:
    print("ready in " +str(i))

while i<=10:
    print("starting up in " + str(i))
    i +=1

如何无休止地重新编程,就像10比0,而不是永远的0比10


Tags: orinif编程countdownprintstarting
3条回答

将两个循环放在无限外循环中,例如:

while True:
    for i in range(10,1,-1):
        print(i)

    for i in range(1,10):
        print(i)
count_up = 0
count_down = 10
while True:
    count_up += 1
    count_down -= 1
    print("Count_up: ", count_up)
    print("Count_down:", count_down)
    if count_up == 10:  # once it counts up to 10 reset to 0 
       count_up = 0
    if count_down == 0: # once it dounts down to 0 reset to 10
       count_down = 10

这将上升10,下降10 endlessley

或者,如果您只想在本例中使用1个变量i:

i = 10
while True:
    print(i)
    i -= 1 
    if i == 0:
        for j in range(10):
            print(i)
            i += 1

在python中,有一种简单的方法可以完成无休止的循环。您可以使用while(True),它将始终为True。我希望这有帮助

i = 10
temp = True
while(True):
    i = 10
    while i>=1:
        print("count down in " + str(i)
        i -= 1

        if i<10:
            print("blasting off")

            if i==0:
                print("ready in " +str(i))

    while i<=10:
        print("starting up in " + str(i))
        i +=1

相关问题 更多 >