【问题标题】:python guessing random number game not working, my names arent defined?python猜随机数游戏不起作用,我的名字没有定义?
【发布时间】:2019-11-28 18:42:06
【问题描述】:
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(最近一次调用最后一次): 文件“F:\Dominic\Python\rando.py”,第 36 行,在 查看() 文件“F:\Dominic\Python\rando.py”,第 10 行,检查中 打印(猜测,数字) NameError: name 'guess' is not defined

【问题讨论】:

  • 这能回答你的问题吗? Python Global Variables - Not Defined?
  • 变量guess 在两个函数中都是局部的。这就是为什么在check() 中无法识别它,您正在尝试使用未定义的变量。 Python 告诉你:... NameError: name 'guess' is not defined

标签: python random python-3.6


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的问题与 localglobal 变量之间的差异有关。

    在这里,在您的函数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

    【讨论】:

      【解决方案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,而无需将变量设为全局变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-29
        • 1970-01-01
        相关资源
        最近更新 更多