Dylan
Dylan

Reputation: 113

changing an element in a second list at a certain index in python

Hi I have a problem with changing the element of the second list. This program is for making bowling score chart.

I have two lists

rollA = [10, 6, 8, 3, 10, 10, 6, 7, 8, 10]
rollB = [0, 3, 0, 6, 0, 0, 0, 1, 2, 10]

In the program, I add the two lists in the same index, for example, I add 10 with 0, and 6 with 3 and 8 with 0 and so on. I want to make a program that changes the element of rollB into / when the element of the specific index of rollA is not 10, but the added sum of rollA and rollB of the specific index is 10. For example, if I add 8 and 2, the added sum will be 10, and I want to change the 2 into /.

for i in range(0, 10):
    if rollA[i] != 10 and (rollA[i] + rollB[i] == 10):
        rollB[i] == "/"

This is the program I devised so far, but the elements of the second list won't change if I make the code like this. How should I fix this program to function properly?

Upvotes: 1

Views: 51

Answers (6)

The.B
The.B

Reputation: 381

check your demands and then change rollB[idx] = "/"

def my_func(rollA, rollB):
        for idx, elem in enumerate(rollB):
            rollB[idx] = "/" if (elem + rollA[idx] == 10) \
            and (rollB[idx] > 0) else rollB[idx]
        return rollA, rollB
    
    my_func([10, 6, 8, 3, 10, 10, 6, 7, 8, 10], 
            [0, 3, 0, 6, 0, 0, 0, 1, 2, 10])

Upvotes: 0

Maxim
Maxim

Reputation: 286

You can do the following:

rollA = [10, 6, 8, 3, 10, 10, 6, 7, 8, 10]
rollB = [0, 3, 0, 6, 0, 0, 0, 1, 2, 10]
new_list = []

for count, val in enumerate(rollB):
   if val + rollA[count] == 10:
       new_list.append('/')
   else:
       new_list.append(val + rollA[count])

By creating an if and else statement within your enumerated (indexed) for loop.

Upvotes: 0

Kevin Mayo
Kevin Mayo

Reputation: 1159

If you want a comprehension way:

rollA = [10, 6, 8, 3, 10, 10, 6, 7, 8, 10]
rollB = [0, 3, 0, 6, 0, 0, 0, 1, 2, 10]

rollB = ["/" if x + y == 10 and x < 10 else y for x,y in zip(rollA, rollB)]

Upvotes: 0

Mureinik
Mureinik

Reputation: 311073

== is the equality check operator. In order to assign a value, you need to use the = operator:

rollB[i] = "/"

Upvotes: 0

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2675

Using the zip() function we are iterating over rollA and rollB. Then if the conditions you mentioned - the sum of them are 10 and the element is not equal to 10, the element of the rollB is replaced with the /

rollA = [10, 6, 8, 3, 10, 10, 6, 7, 8, 10]
rollB = [0, 3, 0, 6, 0, 0, 0, 1, 2, 10]
for i, j in zip(rollA, rollB):
    if i + j == 10 and i < 10:
        rollB[rollB.index(j)] = '/'

Upvotes: 2

Roshin Raphel
Roshin Raphel

Reputation: 2689

Error is in rollB[i] == "/" it returns either True or False .The assignment symbol is =, not ==. The later one checks for matches. Change your code to :

for i in range(0, 10):
    if rollA[i] != 10 and (rollA[i] + rollB[i] == 10):
        rollB[i] = "/"

Upvotes: 1

Related Questions