python猜随机数游戏没用,我的名字没定义?

2024-09-30 20:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: lenumberinputifdefcheckstartal
3条回答

在Python中,在函数外部定义的变量不能在函数内部访问。同样,函数内部定义的变量不能在函数外部或另一个函数内部访问。例如

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

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

如果你运行这个程序,它会

foo
no foo
not foo

因为这三个f要么在函数外部,要么在函数内部,所以它们都不相同。在程序中,在check()中使用guessnumber,即使这些变量是在start()rand()中定义的。为了能够在任何地方访问这些变量,在程序开始时,必须将

global guess
global number

以便能够从程序的任何地方访问这些变量。必须在定义变量之前放置这些。 另一种解决方法是在程序结束时

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

rand()函数中,将return number放在末尾,在start()函数中,将return guess放在末尾。另外,您必须在check()的参数中

def check(number, guess)

这允许check()函数在其参数中获取numberguess,而不必使变量全局化

你的问题与局部变量和全局变量之间的差异有关

这里,在函数check()中,您引用的是局部变量guess,它只在另一个函数start()中定义,没有在函数check()的上下文中定义。函数check()不知道变量guess,除非您指定它在函数中等于什么

在这种情况下,您可以做的是:

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()

下面是Python文档中关于全局和局部变量的more information

变量guess是函数start的局部变量。这意味着其他函数看不到它。考虑从start返回:


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

同样的语句也适用于函数rand(从函数返回随机数)

然后更改check的定义如下:

def check(guess, number):

最后,启动程序如下:

check(start(), rand())

相关问题 更多 >