如何制作Python循环

问题描述 投票:0回答:1

我需要有关 python 循环的帮助。我试过这个:

yes=1
play = yes
if play == yes:
    import os
    import random
    import time
    n = random.randint(1,10)
    g = 0
    time.sleep(.25)
    print ("guess the number 1-10")
    time.sleep(.5)
    while g != n:
        g = int ( input ( "what is your guess " ))
        if g < n:
            print ("too low guess again")
        if g > n:
            print (" too high guess again")
        print ("you got the number")
    play = input  ("play again? ")
    time.sleep(.5)
    os.system(clear)

有人可以帮助我吗?

我是Python新手,所以我很难让它拥有它,所以如果你得到正确的数字,然后说“是”再次播放,它会清除然后循环。

python loops
1个回答
0
投票

您需要一个外部

while
循环才能一次又一次地玩游戏

import os
import random
import time

play = "yes"
while play == "yes":
    n = random.randint(1, 10)
    g = 0
    time.sleep(.25)
    print("guess the number 1-10")
    time.sleep(.5)
    while g != n:
        g = int(input("what is your guess ?"))
        if g < n:
            print("too low guess again")
        elif g > n:
            print(" too high guess again")
        else:
            print("you got the number")
    play = input("play again? ('yes' to continue) ")
    time.sleep(.5)
    os.system("clear")

如果你用

function

破坏你的代码可能会更容易理解
© www.soinside.com 2019 - 2024. All rights reserved.