首页 > 解决方案 > 是什么导致以下代码中的分配错误之前的引用?

问题描述

我在分配之前收到了这个错误,我不知道如何解决它。

目前我还没有尝试过任何东西。如果能回答这个问题将不胜感激。(我只是想填更多的词,以便可以发布)

这是error code我得到的:

number = int(number)

UnboundLocalError: local variable 'number' referenced before assignment

这是我剩下的代码

import random
import sys

again = True









while True:
    myName = input('Hello, Enter your name to get started')
    if myName.isdigit():
        print('ERROR,Your Name is not a number, please try again')
        print('')
        continue
    break
myName = str(myName.capitalize()) 




print('')    
print('Hi {}, This is Guessing Game, a game where you have a certain amount of attempts to guess a randomly generated number. Each level has a different amount of attempts and a higher range of number. After each guess, press enter and the program will determine if your guess is correct or incorrect.' .format (myName))

print('--------------------------------------------------------------------------------')





while True:
    level = input('{}, Please select a level between 1 and 3. Level 1 being the easiest and 3 being the hardest')
    if not level.isdigit():
        print('Please enter a number between 1 and 3. Do not enter a number in word form')
        continue
    break

def guessNumber(): # Tells the program where to restart if the user wants to play again
    guessesTaken = 0
    List = []



    if level == 1:
        number = random.randint (1, 16)
        print('You chose Level 1, Guess a number a between 1 and 16, you have 6 guesses.') 
        allowedGuesses = 6 
        boundary = 16 

    if level == 2: # The code for level 2
        number = random.randint (1,32)
        print('You chose Level 2, Guess a number between 1 and 32, You have 8 guesses.')
        allowedGuesses = 8
        boundary = 32


    if level == 3:
        number = random.randint (1, 40)
        print('You chose Level 3, Guess a number between 1 and 40, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 40

    if level == 4:
        number = random.randint (1, 50)
        print('You chose Level 4, Guess a number between 1 and 50, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 50

    if level == 5:
        number = random.randint (1, 60)
        print('You chose Level 5, Guess a number between 1 and 60, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 60





        guess = input() 
        guess = int(guess)
        while guessesTaken < allowedGuesses:



            guessesTaken = guessesTaken + 1 
            guessesLeft = allowedGuesses - guessesTaken





        if guess < number:  
            List.append(guess)
            print('Your guess is too low, You must guess a higher number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))


        if guess > number: 
            List.append(guess)
            print('Your guess is too high, You must guess a lower number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))


        if guess > boundary: 
            List.append(guess)
            print('You must input a number between 1 and 16. You have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))

        if guess == number:
            List.append(guess)
            print('Good Job {}!, You guessed my number in {} guesses. You guessed the numbers {}.' .format (myName, guessesTaken, List))
            print('Your high score for your previous game was {}' .format(guessesTaken))





    else:
        number = int(number)
        print('')
        print('--------------------------------------------------------------------------------')      
        print('Sorry {}, Your gueses were incorrect, The number I was thinking of was {}. You guessed the numbers {}.' .format(myName, number, List))







guessNumber()
print('')
print('It is recommended to pick a harder level if you chose to progress')
print('')

while True:
    again = input('If you want to play again press 1, if you want to stop playing press 2')
    if not again.isdigit():
        print('ERROR: Please enter a number that is 1 or 2. Do not enter the number in word form')
        continue
    break





if again == 1:
    level + 1
    guessNumber()






if again == 2:
    print('Thanks for playing Guessing Game :)')
    sys.exit(0)

标签: pythonvariables

解决方案


在您的代码中,您得到levelasinput并检查是否级别在 1 到 5 之间。
否则您正在尝试number = int(number)
,但您应该编写 number = int(level).


推荐阅读