我的python猜谜游戏

时间:2017-12-21 03:01:17

标签: python python-3.x variables

我正在制作一个猜谜游戏,您可以选择简单的中型和硬式模式但是当我调整代码以使显示从100变为1,000,000时,我收到错误说:

File "C:/Users/Zach Hirschman/AppData/Local/Programs/Python/Python35-32/GuessGame.py", line 38, in main
    if guess > randomNumber:
UnboundLocalError: local variable 'randomNumber' referenced before assignment

我似乎无法弄清楚如何解决这个问题,任何帮助都会受到赞赏。

"""
Zach Hirschman
12/20/2017
GuessGame.py

This program asks the user to guess a number
"""

import random

def main():
    difficulty = input("Enter a difficulty level: 'easy','medium', or 'hard' 
: ")
print("At any time, type 'hint' to get a hint")
if difficulty == "easy":
    randomNumber = random.randint(1,100)

elif difficulty == "medium":
    randumNumber = random.randint(1,10000)

elif difficulty == "hard":
    randomNumber = random.randint(1,1000000)

found = False

while not found:
    if difficulty == "easy":
        guess = int(input("Guess a number between 1 and 100"))

        if guess > randomNumber:
            print("Too high")
        elif guess == randomNumber:
            print("Thats correct!!")
            found = True
        else :
            print("Too Low")

    elif difficulty == "medium":
        guess = int(input("Guess a number between 1 and 10000"))

        if guess > randomNumber:
            print("Too high")
        elif guess == randomNumber:
            print("Thats correct!!")
            found = True
        else :
            print("Too Low")

    elif difficulty == "hard":
        guess = int(input("Guess a number between 1 and 1000000"))
        if guess > randomNumber:
            print("Too high")
        elif guess == randomNumber:
            print("Thats correct!!")
            found = True
        else :
            print("Too Low")



x = input("would you like to play again? Type 'yes' or 'no': ")

if x == "yes":
    main()

else:
    print("See you later!")
main()

2 个答案:

答案 0 :(得分:0)

我认为这只是一个范围问题。对于在while循环中可用的randomNumber变量,while循环必须在声明变量的同一函数内(例如main()):那么它们具有相同的“范围”。

不需要在if语句之前初始化randomNumber,并且没有任何区别。这通常在forwhile循环之前完成(就像您对found = False所做的那样)。 randomNumber没有被初始化,因为它取决于“难度”,这超出了范围。

请注意,在定义它之后,该函数必须由main()调用。在你的代码中,main()没有运行,只有ifs / while部分是在函数外写的。

import random

def main():
    difficulty = input("Enter a difficulty level: 'easy','medium', or 'hard' : ")

    print("At any time, type 'hint' to get a hint")
    if difficulty == "easy":
        randomNumber = random.randint(1,100)

    elif difficulty == "medium":
        randomNumber = random.randint(1,10000)

    elif difficulty == "hard":
        randomNumber = random.randint(1,1000000)

    found = False

    while not found:
        if difficulty == "easy":
            guess = int(input("Guess a number between 1 and 100"))

            if guess > randomNumber:
                print("Too high")
            elif guess == randomNumber:
                print("Thats correct!!")
                found = True
            else :
                print("Too Low")

main()

其他选项包括:

  • 将所有内容从函数中移出,这对于更长的程序来说会很复杂
  • 向main()添加返回值并将其绑定到randomNumber。对于难度变量,这也需要相同,因此最好重构为多个较小的函数。

最后,randomNumber有一个拼写错误 - 虽然没有引起问题。

答案 1 :(得分:0)

另一种简化您的解决方案的方法:

import random

difficulty_thresholds = {
    "easy" : 100,
    "medium" : 10000,
    "hard" : 1000000
    }

wants_to_continue = True
while wants_to_continue:
    difficulty = input("Enter a difficulty level: 'easy','medium', or 'hard': ")
    maximum_number = difficulty_thresholds.get(difficulty, 100)
    randomNumber = random.randint(1, maximum_number)

    found = False
    while not found:
        guess = int(input("Guess a number between 1 and {}: ".format(maximum_number)))

        if guess > randomNumber:
            print("Too high")
        elif guess == randomNumber:
            print("That's correct!!!")
            found = True
        else:
            print("Too Low")

    do_we_continue = input("Would you like to play again? Type 'yes' or 'no': ")

    wants_to_continue = do_we_continue  == "yes"

print("See you later!")