0
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

  • Does this answer your question? [Python Global Variables - Not Defined?](https://stackoverflow.com/questions/49715854/python-global-variables-not-defined) – Celius Stingher Nov 28 '19 at 18:47
  • The variable `guess` is local in both functions. This is why it is not recognised in `check()`, you are trying to use a variable which is not defined. Python is telling you that: `... NameError: name 'guess' is not defined` – Celdor Nov 28 '19 at 18:49

3 Answers3

0

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.

bglbrt
  • 2,018
  • 8
  • 21
0

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())
Oliver W.
  • 13,169
  • 3
  • 37
  • 50
0

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.

dev-gm
  • 63
  • 1
  • 4