2017-09-20 34 views
1

本学期我正在学习Python,这是我的作业。在Python 2中使用输入后出现NameError

有谁能告诉我为什么我的代码错了吗?

问题:
你想知道在计算机科学的品位,所以写一个程序,将连续拍摄0和100之间的等级标准输入,直到你输入“停”,此时它应该打印平均到标准输出。

我的代码:

total=0 
count=0 

while True: 

    grade=input("Enter Your Grades between 0 and 100 [put 'stop' when done]:") 

    if grade<0 or grade>100: 
     print("Invalid Input") 
     continue 

    elif grade=="stop": 
     break 

    else: 
     count+=1 
     total+=grade 

print "Your Average Grade is:"+format(total/count,'.2f') 

当我运行的代码,Python的不断给我这个消息:

Screenshot

+0

你错过了在while循环缩进(通常为4位)以及在最后的打印函数调用周围使用括号。原来是那些疏忽? – synchronizer

+0

这是Python 2还是3代码?你不能在没有括号的情况下在Python 3和Python 2中使用'print',你应该使用'raw_input'而不是'input'。 – skrx

+0

你也在比较一个字符串(从input()返回)和ints。 – synchronizer

回答

0

你在Python 2,其中input运行此程序评估用户输入。因此,如果你输入“stop”,Python会尝试找到不存在的变量stop,并引发NameError

还有更多的问题,你需要重构代码。您应该做的第一件事是将input更改为raw_input,它只是将用户输入作为字符串返回。然后你需要检查用户是否输入“stop”并中断,否则将输入字符串转换为int,然后递增counttotal。 总= 0 数= 0

while True: 
    grade = raw_input("Enter Your Grades between 0 and 100 [put 'stop' when done]:") 
    if grade == "stop": 
     break 
    # Skip if the string can't be converted to an int. 
    if not grade.isdigit(): 
     print("Invalid Input") 
     continue 
    # Now convert the grade to an int. 
    grade = int(grade) 

    if grade < 0 or grade > 100: 
     print("Invalid Input") 
     continue 
    else: 
     count += 1 
     total += grade 

# Convert total to a float for true division. 
print "Your Average Grade is: {:.2f}".format(float(total)/count) 
+0

感谢您的澄清。在Python 2中学习了更多的操作。顺便说一句,我重新安装了Python 3. –

+0

啊,这很好。在Python 3中'输入'像'raw_input'一样工作(返回字符串并且不评估输入(这将是不安全的)),并且'/'执行“真正的划分”(float division),所以你不必转换其中一个操作数为浮点数,可以只写'total/count'。另外,为了将字符串转换为整数和浮点数,我们通常使用['try:...除了ValueError:'](https://docs.python.org/3/tutorial/errors.html#handling-exceptions)子句防止错误。 – skrx

0

变化inputraw_input

grade = raw_input("Enter Your Grades between 0 and 100 [put 'stop' when done]:") 
0

您正在使用Python 2.7,所以使用raw_input代替input。后者评估输入,所以5 + 2将返回7而不是字符串'5 + 2'。输入stop会尝试将stop评估为不存在的变量。

另一个注意事项,totalcount都是整数,所以total/count在Python 2中执行整数除法(Python 3给出了浮点结果)。如果你想要一个浮点平均值,使用float(total)/count。其中一个变量必须浮动以获得浮动答案。

你还会发现,grade是一个字符串,所以测试'stop'第一,然后将其转换为int测试等级grade = int(grade)。你可能想考虑处理错误。如果用户输入10a会怎么样?

0

您可以评估第一串停止,尝试捕获输入与raw_input

total = 0 

count = 0 

while True: 

    grade = raw_input("Enter Your Grades between 0 and 100 [put 'stop' when done]:") 

    if grade == "stop": 
     break 
    if grade.isdigit(): 
     grade = int(grade) 
     if grade < 0 or grade > 100: 
      print("Invalid Input") 
      continue 
     else: 
      count += 1 
      total += grade 

if count == 0: 
    print "Empty values" 
else: 
    print "Your Average Grade is: %.2f" % (float(total)/count) 

我添加了正确执行不同的条件,检查线路,例如if grade.isdigit():用于验证输入值是数字价值,当这个评估我们可以正常使用任何数学计算。

count == 0:错误除以零,如果用户在第一次迭代中写入stop

在最后一行中,你可以使用两种不同的方式来打印值:

print "Your Average Grade is: %.2f" % (float(total)/count) 

print "Your Average Grade is: {:.2f}".format(float(total)/count) 
+0

downvote?为什么?也许是不好的添加很多代码? – kip

+0

也许是因为我们忘记将其中一个操作数转换为浮点数以获得真正的分区?如果没有解释,就会瘫痪。 – skrx

+0

感谢您的澄清。我重新安装了Python 3。 –