繁体   English   中英

python猜测随机数游戏不起作用,我的名字没有定义?

[英]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()

回溯(最近一次调用):文件“F:\\Dominic\\Python\\rando.py”,第 36 行,check() 文件“F:\\Dominic\\Python\\rando.py”,第 10 行,检查打印(猜测,数字)NameError:未定义名称“猜测”

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

在这里,在您的函数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 文档中有关全局和局部变量的更多信息

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

在 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 ,而无需将变量设为全局变量。

暂无
暂无

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

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