在if语句内启动函数时出错

2024-10-02 02:26:53 发布

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

我有一个在if函数中运行的def,但是当它应该启动时,它却没有。下面是我写的代码的副本。在我为两场比赛准备if语句之前,它就像一个def一样运行良好

print("Please choose from one of the following: \n")
print("1. Tic-Tac-Toe. \n")
print("2. Number Guessing Game. \n")
playerQuestion = input("")
print("You have selected game " + playerQuestion + "! Starting the game up. . . ")
sleep(2)
if playerQuestion == 1:
    print("1")


    def guessingGame():

        for i in range(5):
            print("")
            playerGuess = ()
            computerNumber = (randrange(100))

            playerGuess = int(input("I am thinking of a number... Please make your first guess! "))

            tries = 1

        while playerGuess != computerNumber:
            tries += 1
            if (playerGuess > computerNumber):
                print("Lower! \n")
                playerGuess = ()
                playerGuess = int(input("I am thinking of a number... Please make your next guess! "))
            elif (playerGuess < computerNumber):
                print("Higher! \n")
                playerGuess = ()
                playerGuess = int(input("I am thinking of a number... Please make your next guess! "))
        if playerGuess == computerNumber:
            tries = str(tries)
            print("You got it in " + tries + " tries! Congratulations! ")
            again = str(input("Do you want to play again (type yes or no): "))
            again = again.lower()
            again = again[:1]
            if again == "y":
                guessingGame()
            else:
                sleep(1)
                print("Thank you for playing! ")


    guessingGame()

    sleep(10)
else:
    def TicTacToe():

        board = [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
        ]
        for i in range(5):
            print(board)

Tags: ofinforinputifdefsleepprint

热门问题