繁体   English   中英

我的python猜谜游戏存在变量问题

[英]Variable issue with my python guessing game

我正在做一个猜谜游戏,您可以选择简单的中硬模式,但是当我调整代码以使显示从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()

我认为这只是一个范围界定的问题。 为了在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有一个错字-但这并没有引起问题。

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

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!")    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM