Im creating a program that takes input scores, adds them to a list and, using a for-loop, adds them all together displaying the total. Getting some problems though. Check it out Please..
scoreList = []
count = 0
score = 0
sum = 0
while score != 999:
score = float(input("enter a score or enter 999 to finish: "))
if score > 0 and score < 100:
scoreList.append(score)
elif (score <0 or score > 100) and score != 999:
print("This score is invalid, Enter 0-100")
else:
for number in scoreList:
sum = sum + scoreList
print (sum)
The problem is simple:
for number in scoreList:
sum = sum + scoreList
If you want to add each number in scoreList, you have to add number
, not scoreList
:
for number in scoreList:
sum = sum + number
Otherwise, you're trying to add the whole list to sum
, over and over, once per value. Which is going to raise a TypeError: unsupported operand type(s) for +: 'int' and 'list'
… but really, there's nothing it could do which could be what you want.
A simpler solution is to use the built-in sum
function. Of course that would mean you need a different variable name, so you don't hide the function. So:
total_score = sum(scoreList)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.