NameError:name''没有定义?

时间:2015-02-03 14:54:55

标签: python

我已经搜索了,这已经得到了很多回答,但它主要与版本错误有关。我检查了我的Python版本(它是3.4.2),但它仍然返回错误。

代码错误:

import random

def start():
    rannum = random.randint(0, 20)
    inpnum = int(input('Please enter a number between 0 and 20: '))
    calc()

def loop():
    inpnum = int(input('Please enter a number between 0 and 20: '))
    calc()

def calc():

    if inpnum > rannum:
        print('The number you entered is too high.')
        loop()

    if inpnum < rannum:
        print('The number you entered is too low.')
        loop()

    if inpnum == rannum:
        print('Congratulations! You guessed the number!')
        input('')

start()

这将返回错误代码:

Traceback (most recent call last):
  File "guessthenumber.py", line 26, in <module>
    start()
  File "guessthenumber.py", line 6, in start
    calc()
  File "guessthenumber.py", line 14, in calc
    if inpnum > rannum:
NameError: name 'inpnum' is not defined

我自己尝试修复它并将其更改为:

import random

def loop():
    inpnum = int(input('Please enter a number between 0 and 20: '))
    calc()

def calc():

    rannum = random.randint(0, 20)
    inpnum = int(input('Please enter a number between 0 and 20: '))

    if inpnum > rannum:
        print('The number you entered is too high.')
        loop()

    if inpnum < rannum:
        print('The number you entered is too low.')
        loop()

    if inpnum == rannum:
        print('Congratulations! You guessed the number!')
        input('')

calc()

它确实有用,但它会不断创建一个新数字(我想我知道为什么,每次运行时,rannum = random.randint(0,20)一直在编造一个新数字)并且它会要求输入两次(因为它已经在循环()中请求一个但是在calc()中再次这样做,我想)所以你必须随机猜测,直到你拥有它,因为它不断改变每一个猜测。

对我来说,它看起来像完整的Python代码。有没有其他人知道该怎么做?

4 个答案:

答案 0 :(得分:2)

一种解决方案是将calc函数更改为接受arguemnt:

def calc(num):
    # do stuff with num

并将ipnum传递给它:

def loop():
    inpnum = int(input('Please enter a number between 0 and 20: '))
    calc(inpnum)

这是你在另一个函数中的一个函数中使用局部变量的方法。

答案 1 :(得分:0)

您可以将代码简化为一个函数,如下所示,您可以解决范围问题:

示例:

import random

def calc():
    rannum = random.randint(0, 20)         
    inputnum = None
    while inputnum != rannum:
        inpnum = int(input('Please enter a number between 0 and 20: '))
        if inpnum > rannum:
            print('The number you entered is too high.')
        elif inpnum < rannum:
            print('The number you entered is too low.')

    print('Congratulations! You guessed the number!')

calc()

答案 2 :(得分:0)

你走了;

import random

def ask():
    rannum = random.randint(0, 20)
    print "Num: {}".format(rannum)
    while True:
        inpnum = int(input('Please enter a number between 0 and 20: '))
        if inpnum > rannum:
            print('The number you entered is too high.')
        elif inpnum < rannum:
            print('The number you entered is too low.')
        else:
            print('Congratulations! You guessed the number!')
            break

ask()

结果

$ python lol.py
Num: 18
Please enter a number between 0 and 20: 1
The number you entered is too low.
Please enter a number between 0 and 20: 20
The number you entered is too high.
Please enter a number between 0 and 20: 18
Congratulations! You guessed the number!

答案 3 :(得分:0)

这很有效。我把print()放在线之间以便看起来更好。

import random

def loop():
    rannum = random.randint(0, 20)

    while True:

        inpnum = int(input('Please enter a number between 0 and 20: '))
        print ()
        if inpnum == rannum:
            print('Congratulations! You guessed the number!')
            print()
            break

        if inpnum > rannum:
            print('The number you entered is too high.')
            print()
            continue

        if inpnum < rannum:
            print('The number you entered is too low.')
            print()
            continue




loop()