我如何创建级别,重播并跟踪获胜和失败

时间:2019-01-20 19:53:09

标签: python

import random

print("Welcome to the game, Guess a Number! \n")
print("There will be 3 rounds to the game.")
print("During the first round, you will be asked to choose a number between 0-10.")
print("You will have 3 chances to guess the correct number. If you can't, the game will start over.")
print("Good Luck! \n")


def ReadyToPlay():
    NumberGuesses = 0 #sets numerb of guesses
    MaxGuesses = 3 #sets maximum times you can guess


    number = random.randint(0,10)
    print("Hi stranger, I am thinking of a number between 0 and 10.")


    while NumberGuesses < MaxGuesses:
        guess = int(input("Take a guess: ")) #prompt to take guess

        NumberGuesses = NumberGuesses + 1 #keeps number count

        if guess < number:
            print("Sorry, your guess is to low.")

        elif guess > number:
                print("Sorry, your guess is to high.")

        if guess == number: #stops current loop and continues the rest of my statment
            break

    if guess == number:
        NumberGuesses = str(NumberGuesses)
        print('\n')
        print("Congrats Stranger! You have guessed the correct number.")
        print("Moving on to round two. \n")



        NumberGuesses = 0 #sets numerb of guesses
        MaxGuesses = 3 #sets maximum times you can guess

        number = random.randint(0,50)
        print("Now, I am thinking of a number between 0 and 50.")


        while NumberGuesses < MaxGuesses:
             guess = int(input("Take a guess: ")) #prompt to take guess

             NumberGuesses = NumberGuesses + 1 #keeps number count

             if guess < number:
                print("Sorry, your guess is to low.")

             elif guess > number:
                    print("Sorry, your guess is to high.")

             if guess == number:
                 break


        if guess == number:
            NumberGuesses = str(NumberGuesses)
            print('\n')
            print("You're on fire!")
            print("Now you have arrived to the last round.")
            print("You will have to choose 2 numbers, in order to complete the game. \n")

            NumberGuesses = 0 #sets numerb of guesses
            MaxGuesses = 6 #sets maximum times you can guess

            number = random.randint(0,100)
            print("The first number I am thinking of is between 0 and 100")


            while NumberGuesses < MaxGuesses:
                guess = int(input("Take a guess: "))

                NumeberGuesses = NumberGuesses + 1

                if guess < number:
                    print("Sorry, your guess is to low.")

                elif guess > number:
                    print("Sorry, your guess is to high.")

                if guess == number:
                    break


            if guess == number:
                print('\n')
                print("Your guessA is correct.")
                print("Now, guess the second number.") 

                NumberGuesses = 0 #sets numerb of guesses
                MaxGuesses = 6 #sets maximum times you can guess

                number = random.randint(0,100)
                print("The second number I am thinking of is between 0 and 100")


                while NumberGuesses < MaxGuesses:
                    guess = int(input("Take a guess: ")) #prompt to take guess

                    NumberGuesses = NumberGuesses + 1 #keeps number count

                    if guess < number:
                        print("Sorry, your guess is to low.")

                    elif guess > number:
                        print("Sorry, your guess is to high.")

                    if guess == number:
                        break


                if guess == number:
                    print('\n')
                    print("Your guessB is also correct.")
                    print("Congratulations on finishing the game!!!") 


    if guess != number:
        number = str(number)
        print("\n")
        print("The number I was thinking of was: " + number)
        replay = input("Do you want to play again (y/n)?: ")
        if replay == "y":
            ReadyToPlay()
ReadyToPlay

我需要帮助创建3个关卡,能够从每个关卡重玩游戏,并跟踪我的得失。我是一个初学者,所以它需要非常容易编码。到目前为止,我已经为游戏创建了三个部分,所有部分都可以正常工作。但是当我需要回复时,游戏从头开始,而不是从您到达的地方开始

1 个答案:

答案 0 :(得分:1)

使用一个可以保存游戏状态的容器;在玩游戏时更新状态。向ReadyToPlay()添加一个可选参数。该参数接受保存当前游戏状态的容器。如果在调用该函数时通过,则可以从那里开始游戏。通过状态容器进行重放


您可能要考虑让函数返回猜测值和当前状态,然后将if guess != number:代码移到函数之外。