'How to Add Win/Loss Counter to Rock Paper Scissors Program

I have pretty much finished this Rock Paper Scissors program, but one of the final steps is to add a counter to print the Win, Loss, and Tie ratio after the user is done playing.

I tried in the count_win_loss() function to enumerate the messages index that I pass to it in the play_game() function, but all it is returning zeros

import random
def count_win_loss(messages_from_result):
    player_wins = 0
    cpu_wins = 0
    ties = 0
    #This is supposed to get the index value position of this list, which 
    should be defined at the end of `play_game()` function.
    for index in enumerate(messages_from_result):
        new_index = index
    #NOT WORKING NEEDS TO BE FIXED
    if new_index == 0:
        ties += 1
    elif new_index == 1:
        player_wins += 1
    elif new_index == 2:
        cpu_wins += 1
    else:
        ties += 0
        player_wins += 0
        cpu_wins += 0
    #NOT WORKING NEEDS TO BE FIXED
    print(player_wins)
    print(cpu_wins)
    print(ties)
    #print('\nHuman Wins: %d, Computer Wins: %d, Ties: %d' % (player_wins, cpu_wins, ties))

This elif statement appears at the end of my game function. It executes when a user inputs '2' which ends the loop.

       #Creates a format that can be passed to the results matrix.
       #Additionally creates an index 3 that I will reference as the error value.
            guesses_index = guess_dict.get(user_guess, 3)
            computer_index = guess_dict.get(computer_guess)
            result_index = results[guesses_index][computer_index]
            final_result = result_messages[result_index]
        
        elif play_again == '2':
            count_win_loss(result_messages) #NOT WORKING NEED HELP
            print('Thanks for playing!')
            continue_loop = False

I pass it this messages list:

result_messages = [
        "You tied!", 
        "You win!", 
        "The Computer won. :(", 
        "Invalid weapon, please try again."
    ]

As mentioned in the title, I need a win/loss counter, which I thought my calc_win_loss() would do



Solution 1:[1]

New_Index variable is not defined inside this function, therefore global to where ever you defining the value and get in the starting of this function For ex:- def a():
b = 2
def c(): . print(b)
. Whenever you will call c() it will cause an error because b is a local variable in side function a() however if you do def a(): global b
b = 2 def c(): print(b)
Now when you call a() and then c(). Th error will go away

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1