在我的猜测它告诉我我的数字变量未定义的数字

时间:2018-04-22 09:31:37

标签: python

我猜数字程序,当我尝试运行它时告诉我变量"数字"没有定义。如果有人来帮助我,我将不胜感激并感激不尽!

import random
guesses = 0


def higher(guesses):
    print("Lower")
    guesses = guesses + 1


def lower(guesses):
    print("Higher")
    guesses = guesses + 1


def correct(guesses):
    print("You got it correct!")
    print("It was {0}".format(number))
    guesses = guesses + 1
    print ("It took you {0} guesses".format(guesses))


def _main_(guesses):
    print("Welcome to guess the number")
    number = random.randint(1, 100)
    while True:
        guess = int(input("Guess a number: "))
        if guess > number:
            higher(guesses)
        elif guess < number:
            lower(guesses)
        elif guess == number:
            correct(guesses)
            while True:
                answer = input("Would you like to play again? Y or N: ")
                if answer == "Y":
                    break
                elif answer == "N":
                    exit()
                else:
                    exit()


_main_(guesses)

2 个答案:

答案 0 :(得分:1)

您的问题是number未在函数correct中定义。 number中定义了_main_。当您在correct中致电_main_时,它无法访问number。 这是您的代码的固定版本:

    import random
    guesses = 0
    number = random.randint(1, 100)


    def higher(guesses):
        print("Lower")
        guesses = guesses + 1


    def lower(guesses):
        print("Higher")
        guesses = guesses + 1


    def correct(guesses):
        print("You got it correct!")
        print("It was {0}".format(number))
        guesses = guesses + 1
        print ("It took you {0} guesses".format(guesses))


    def _main_(guesses):
        print("Welcome to guess the number")
        while True:
            guess = int(input("Guess a number: "))
            if guess > number:
                higher(guesses)
            elif guess < number:
                lower(guesses)
            elif guess == number:
                correct(guesses)
                while True:
                    answer = input("Would you like to play again? Y or N: ")
                    if answer == "Y":
                        break
                    elif answer == "N":
                        exit()
                    else:
                        exit()


    _main_(guesses)

我改变的是我将number的定义移到了顶部,这使得它可以被模块中的所有函数访问。

另外,你的代码风格不是很好。首先,不要为主要功能_main_命名,而是使用main。此外,您不需要打印“低”和“高”的功能。这是一些改进的代码:

import random

def main():
    number = random.randint(1, 100)
    guesses = 0
    while True:
        guessed_num = int(input('Guess the number: '))
        guesses += 1
        if guessed_num > number:
            print('Guess lower!')
        elif guessed_num < number:
            print('Guess higher!')
        else:
            print('Correct!')
            print('The number was {}'.format(number))
            print('It took you {} guesses.'.format(guesses))
            break
main()

答案 1 :(得分:0)

您的具体问题是函数number中未定义变量correct()。可以通过将number作为参数传递给correct()来解决。

但即使你纠正了这个问题,你的程序也有另一个重大问题。您已全局定义guesses,但仍将guesses作为参数传递给lower()higher()correct()。这会在这些函数的范围内创建一个重复的变量guesses,并且每次调用这些函数中的任何一个时,都是这个重复的变量,而不是您在全局创建的变量。因此,无论用户采取多少猜测,它都将始终打印

  

你猜了一次。

<强>解决方案
定义没有参数的函数lower()higher()。告诉那些函数,最终这个代码应该起作用:

import random
guesses = 0


def higher():
   global guesses
   print("Lower")
   guesses = guesses + 1


def lower():
    global guesses
    print("Higher")
    guesses = guesses + 1

def correct(number):
    global guesses
    print("You got it correct!")
    print("It was {0}".format(number))
    guesses = guesses + 1
    print ("It took you {0} guesses".format(guesses))

def _main_():
    print("Welcome to guess the number")
    guesses = 0
    number = random.randint(1, 100)
    while True:
        guess = int(input("Guess a number: "))
        if guess > number:
            higher()
        elif guess < number:
            lower()
        elif guess == number:
            correct(number)
            while True:
                answer = input("Would you like to play again? Y or N: ")
                if answer == "Y":
                    _main_()
                elif answer == "N":
                    exit()
                else:
                    exit()
_main_()