我究竟做错了什么?请

时间:2017-02-28 21:31:21

标签: python

我做错了什么?

num = []
inp = raw_input("please enter a series of numbers separated by a space\n")
num = [ int(i) for i in inp.split()]
print(num)

quit_program = input("Would you like the average or would you like to quit?")
if quit_program == quit:
  quit()
elif quit_program == average:
  print mean(num)

一直说这个

quit_program = input("Would you like the average or would you like to quit?")
  File "<string>", line 1, in <module>
NameError: name 'average' is not defined

2 个答案:

答案 0 :(得分:1)

您的堆栈跟踪说:

在线:quit_program = input("<snip>")当您输入 NameError: name 'average' is not defined时,它会收到错误average

Python2.7中的

input()运行你输入的任何内容(相当于eval(raw_input())。在这种情况下,它试图运行用户输入的average。你只需要{{ 3}}

然后你想将这个输入与字符串进行比较,正如人们所建议的那样:

quit_program = raw_input("Would you like the average or would you like to quit?")
if quit_program == "quit":
    quit()
elif quit_program == "average":
    print mean(num)

答案 1 :(得分:0)

您还没有定义退出和平均值。 您正在检查用户输入,这是一个字符串。 字符串需要&#34;引用&#34;在他们旁边。 这很好:     &#34;我是一个字符串&#34; 这是不好的:     我不是一个字符串