处理范围检查和列表的Python 3 Float错误

时间:2018-09-07 03:38:03

标签: python python-3.x

我正在上第一门Python课程。对于我们的作业,我们应该收集6个等级,如果它们是平均值,则检查它们是否在0-100范围内,如果没有给出错误消息。然后我们应该要求他们的总成绩并做同样的事情。所以我本质上已经将我的代码的一部分(正在运行)复制为第二部分,但是现在我在类型调用中遇到错误,并且我不知道为什么。你们可以帮我弄清楚我出了什么问题,并指出正确的方向。这是我的代码:

six_grade_list = []
print("Please enter six grades using a comma to separate them to get your average.")
six_grade_list = [float(i) for i in input().split(',')]
for float in six_grade_list:
    if 0<= float <= 100:
        print("Your average is ", sum(six_grade_list)/len(six_grade_list))
        break
    else:
        print("One of the grades you entered is not between 0 and 100.")
        break
total_grade_list = []
print("This time please enter all of your grades using a comma to separate them to get your average.")
total_grade_list = [float(i) for i in input().split(',')]
for float in total_grade_list:
    if 0<= float <= 100:
        print("Your average is ", sum(total_grade_list)/len(total_grade_list))
        break
    else:
        print("One of the grades you entered is not between 0 and 100.")
        break

还有我得到的错误:

Traceback (most recent call last):
  File "C:/Users/jesse/PycharmProjects/untitled/CYBR-260-40A/Week2AlternativeAssignment.py", line 13, in <module>
    total_grade_list = [float(i) for i in input().split(',')]
  File "C:/Users/jesse/PycharmProjects/untitled/CYBR-260-40A/Week2AlternativeAssignment.py", line 13, in <listcomp>
    total_grade_list = [float(i) for i in input().split(',')]
TypeError: 'float' object is not callable

先谢谢了。

1 个答案:

答案 0 :(得分:1)

您正在float循环中使用float变量覆盖for函数。将float变量重命名为其他名称,您的代码将起作用:

print("Please enter six grades using a comma to separate them to get your average.")
six_grade_list = [float(i) for i in input().split(',')]
for grade in six_grade_list:
    if 0<= grade <= 100:
        print("Your average is ", sum(six_grade_list)/len(six_grade_list))
        break
    else:
        print("One of the grades you entered is not between 0 and 100.")
        break
print("This time please enter all of your grades using a comma to separate them to get your average.")
total_grade_list = [float(i) for i in input().split(',')]
for grade in total_grade_list:
    if 0<= grade <= 100:
        print("Your average is ", sum(total_grade_list)/len(total_grade_list))
        break
    else:
        print("One of the grades you entered is not between 0 and 100.")
        break