NewUser123
NewUser123

Reputation: 35

Why will my Python list not sum the values together?

frames = []
frame_total = []
total = 0

while len(frames) < 2:
    ball_one = int(raw_input('Enter the score of ball one: '))
    ball_two = int(raw_input('Enter the score of ball two: '))


    frame_total.append([ball_one, ball_two])
    for frame in frame_total:
        frames.append(frame_total)
        b = sum(frames)
print(frame_total)
print(b)

This is my code. I cant work out why I get the error TypeError: unsupported operand type(s) for +: 'int' and 'list' when I try and print out the sum of each array within the main array. any ideas?

the idea is to create a bowling game. you have 2 balls in every frame, ball_one and ball_two. after each frame, i append these 2 numbers to an array. so if ball_one is 8 and ball_two is 1. then it will add [[8, 2]] then the next go if b_1 is 4 and b_2 is 4 then it will look like [[8, 2], [4, 4]]. then I want to add 8 and 2 and 4 and 4 to produce. [[10, 8]]. and then finally the total of both of these, so 18.

Upvotes: 0

Views: 368

Answers (5)

jtitusj
jtitusj

Reputation: 3084

Let me try. You want to append each pair [ball_one, ball_two] into the frames. After that, get the sum per pair and store it in another list, say frames_total and finally display the sum of everything. Here's the code:

frames = [] # will contain raw results in each frame. (ex. [[8, 2], [4, 4]])
frames_total = [] # will contain total values in each frame (ex. [10, 8])
total = 0 # sum of everything

while len(frames) < 2:
    ball_one = int(raw_input('Enter the score of ball one: '))
    ball_two = int(raw_input('Enter the score of ball two: '))

    frames.append([ball_one, ball_two])

frames_total = [sum(frame) for frame in frames]
total = sum(frames_total)

print frames_total
print total

If you want to have the info printed after every turn, here's a slightly different code:

frames = [] # will contain raw results in each frame. (ex. [[8, 2], [4, 4]])
frames_total = [] # will contain total values in each frame (ex. [10, 8])
total = 0 # sum of everything

while len(frames) < 2:
    ball_one = int(raw_input('Enter the score of ball one: '))
    ball_two = int(raw_input('Enter the score of ball two: '))

    frames.append([ball_one, ball_two])
    frames_total.append(ball_one + ball_two)
    total = sum(frames_total)

    print frames
    print frames_total
    print total

Upvotes: 0

zondo
zondo

Reputation: 20346

  • You keep defining b in each iteration of the for loop. You should wait to define it until you actually need it.

  • You keep appending frame_total to frames instead of frame.

  • You try to find the sum of a list of lists instead of all the numbers inside each list.

Your updated program:

frame_total = []

while len(frames) < 2:
    ball_one = int(raw_input('Enter the score of ball one: '))
    ball_two = int(raw_input('Enter the score of ball two: '))

    frame_total.append([ball_one, ball_two])

print(frame_total)
print(sum(sum(sub) for sub in frame_total))

If you don't like generator expressions:

frame_total = []
total = 0

while len(frames) < 2:
    ball_one = int(raw_input('Enter the score of ball one: '))
    ball_two = int(raw_input('Enter the score of ball two: '))

    frame_total.append([ball_one, ball_two])
    total += ball_one + ball_two

print(frame_total)
print(total)

Upvotes: 1

Arshid KV
Arshid KV

Reputation: 10037

Change it as follows:

frames = []
frame_total = []
total = 0

while len(frames) < 2:
    ball_one = int(input('Enter the score of ball one: '))
    ball_two = int(input('Enter the score of ball two: '))


    frame_total.append(ball_one)
    frame_total.append(ball_two)
    b = 0
    for frame in frame_total:
        frames.append(frame_total)
        b = b + frame
print(frame_total)
print(b)

Upvotes: 0

user4754843
user4754843

Reputation:

Your frame_total has one element, and it is [ball_one, ball_two]. When you append frame_total to frames, frames now has one element and it is still [ball_one, ball_two]. When you sum(frames) it will raise a TypeError because frames element is a list not an int.

Upvotes: 0

Sel&#231;uk Cihan
Sel&#231;uk Cihan

Reputation: 2034

From the documentation

sum(iterable[, start])

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.

You are calling sum with a list of list. And since you have not supplied start, it is zero by default. So it basically tries to add an int and a list.

Try running 0 + [1,2,3] and you will understand what your error is.

Upvotes: 0

Related Questions