简体   繁体   中英

python guessing random number game not working, my names arent defined?

import sys, random
def rand():
    number = random.randint(0, 100)

def start():
    print("Entrez un nombre et essayez de faire correspondre le nombre aléatoire")
    guess= int(input())

def check():
    print (guess, number)
    if guess == number:
        print ("Les nombres sont le même!")
        print ("Recomence?")
        reawn=str(input())
        if reawn == "oui":
            rand()
            start()
            check()
    elif guess < number:
        print ("Ton nombre est plus grands que le nombre aléatoire!")
        print ("Essaye encore?")
        reawn=str(input())
        if reawn == "oui":
            start()
            check()
    elif guess > number:
        print ("Ton nombre est plus petit que le nombre aléatoire!")
        print ("Essaye encore?")
        reawn=str(input())
        if reawn == "oui":
            start()
            check()

rand()
start()
check()

Traceback (most recent call last): File "F:\\Dominic\\Python\\rando.py", line 36, in check() File "F:\\Dominic\\Python\\rando.py", line 10, in check print (guess, number) NameError: name 'guess' is not defined

Your problem has to do with the difference between local and global variables.

Here, in your function check() , you're refering to the local variable guess which was only defined inside the other function start() and has not been defined in the context of the function check() . The function check() does not know the variable guess unless you specify what it's equal to inside the function.

What you could do in this case is:

import sys, random

def rand():
    number = random.randint(0, 100)
    return number

def start():
    print("Entrez un nombre et essayez de faire correspondre le nombre aléatoire")
    guess= int(input())
    return guess

def check():

    number = rand()
    guess = start()

    print (guess, number)
    if guess == number:
        print ("Les nombres sont le même!")
        print ("Recomence?")
        reawn=str(input())
        if reawn == "oui":
            rand()
            start()
            check()
    elif guess < number:
        print ("Ton nombre est plus grands que le nombre aléatoire!")
        print ("Essaye encore?")
        reawn=str(input())
        if reawn == "oui":
            start()
            check()
    elif guess > number:
        print ("Ton nombre est plus petit que le nombre aléatoire!")
        print ("Essaye encore?")
        reawn=str(input())
        if reawn == "oui":
            start()
            check()

rand()
start()
check()

Here's more information on global and local variables from the Python documentation.

The variable guess is local to the function start . That means other functions don't see it. Consider returning it from start :


def start():
    print("Entrez un nombre et essayez de faire correspondre le nombre aléatoire")
    guess= int(input())
    return guess

Same statement goes for your function rand (return the random number from the function).

Then change the definition of check as follows:

def check(guess, number):

Finally, start the program as such:

check(start(), rand())

In Python, variables defined outside of a function cannot be accessed inside a function. In the same way, variables defined inside of a function cannot be accessed outside the function or within another function. For example,

f="foo"
def function1():
    f="no foo"
    print(f)
def function2():
    f="not foo"

    print(f)
print(f)
function1()
function2()

If you run this program, it will come out to

foo
no foo
not foo

because the three f 's are either outside of the function or inside of a function, none of them are the same. In your program, you use guess and number in check() even though those variables were defined in start() and rand() . In order to be able to access these variables anywhere, at the start of your program, you must put

global guess
global number

in order to be able to access those variables from anywhere in the program. You must place these before the variable is defined. Another way you could go about to fix this is to do at the end of your program,

number = rand()
guess = start()
check(number, guess)

and in the rand() function, put return number at the end, and in the start() function, put return guess at the end. Also, you have to put, in the parameters for check() ,

def check(number, guess)

This allows the check() function to get number and guess in its parameters without having to make the variables global.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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