代码之家  ›  专栏  ›  技术社区  ›  ghostey

如何制作python循环

  •  0
  • ghostey  · 技术社区  · 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的新手,所以我很难让它拥有它,所以如果你的数字正确,然后说“是”再玩一次,它就会清除,然后循环。

    1 回复  |  直到 1 年前
        1
  •  0
  •   azro    1 年前

    你需要一个外层 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

    def one_run():
        n = random.randint(1, 10)
        g = 0
        print("guess the number 1-10")
        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 = "yes"
    while play == "yes":
        one_run()
        play = input("play again? ('yes' to continue) ")
        time.sleep(.5)
        os.system("clear")