Reputation: 51
I'm in my first Python course. For our assignment we're supposed to collect 6 grades, check them for being in the range of 0-100, if they are find the average, if not give an error message. Then we're supposed ask for their total grades and do the same thing. So I've essentially duplicated one part of my code (which was working) for the second part, but now I'm getting an error for type calling and I have no idea why. Could you guys help me figure out where I went wrong and point me in the right direction. Here's my code:
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
And the error I'm getting:
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
Thanks in advance.
Upvotes: 3
Views: 47
Reputation: 106588
You're overriding the float
function with a float
variable in your for
loops. Rename the float
variable to something else and your code would work:
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
Upvotes: 1