Python Rock - Paper Scissor project - How to increment variables inside a list?

Viewed 108

I am trying my hand a developing the code for this game.

After deciding how many rounds (3, 5 or 7), I ask for input on which move the user will do. After comparison with the ai's choice, I want the score to be displayed each round.

The way I want to display the score is as a list type : score = [ai_score, player_score]. However, whenever I try to increment a player's score, when I call print (score) the list is not updated with the values. Each round the print returns [0, 0]

My question is : Can you put variables in a list and then increment those variables?

Thank you.

Please see code :

import random

ai_score, player_score=0,0
score = [ai_score, player_score]
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))

for round in range(rounds):
    player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
    ai_choice = random.choice(game)
    if player_choice in game:
        if player_choice == ai_choice:
            print("it's a draw")
        elif player_choice == 'R':
            if ai_choice == 'S':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'P':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'P':
            if ai_choice == 'R':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'S':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'S':
            if ai_choice == 'P':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'R':
                ai_score += 1
                print("You loose this round")
        print("This is round %d" % round)
        print("The score is %s" % score)
        print(score)
    else:
        print('Please enter one of the 3 choices')
2 Answers

No, updating the variables will not update the values of the list. To keep the values updated inside the list, you can declare the list inside the loop.

import random

ai_score, player_score = 0, 0
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))

for round in range(rounds):
    player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
    ai_choice = random.choice(game)
    if player_choice in game:
        if player_choice == ai_choice:
            print("it's a draw")
        elif player_choice == 'R':
            if ai_choice == 'S':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'P':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'P':
            if ai_choice == 'R':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'S':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'S':
            if ai_choice == 'P':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'R':
                ai_score += 1
                print("You loose this round")
        print("This is round %d" % round)
        score = [ai_score, player_score]
        print("The score is %s" % score)
        print(score)
    else:
        print('Please enter one of the 3 choices')

Output:

Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?3
Please choose your move : R for rock, P for paper, S for scissors :R
You win the round!
This is round 0
The score is [0, 1]
[0, 1]
Please choose your move : R for rock, P for paper, S for scissors :P
You loose this round
This is round 1
The score is [1, 1]
[1, 1]
Please choose your move : R for rock, P for paper, S for scissors :S
You loose this round
This is round 2
The score is [2, 1]
[2, 1]

Explanation:

When we create a list with variables, the list puts the values of those variables; not the memory address. So, updating the variables, later on, will not affect the list.

In the above scenario, we have assigned initial score to zero for ai_score and player_score using ai_score, player_score = 0, 0. Then, we have assigned the value of these variables to score list using score = [ai_score, player_score]. Later, if we update the values of ai_score and player_score, the score list will not be updated. We need to update the score list with new values of ai_score and player_score.

Original code:

score = [ai_score, player_score]

In order to update any individual item in a list, you add square brackets next to it with the item index within them, then set that equal to whatever value you want to change it to.

In your case, to change the ai score to 5:

score[0] = 5

Where 0 is the index (the index starts a 0 so the player score index would be 1)

Related