0

I am building a rock paper scissors game which can store the number of wins of the player and computer. But i am getting this error name 'winner' is not defined on line 53. How do i fix it? I even tried to define the winner name at the top of the code but still it would say that the name 'winner' is not defined.

    import random

while True:

    player_winnings = 0
    computer_winnings = 0

    choices = ['Rock', 'Paper', 'Scissors']
    player_choice = input("Please enter your choice: ")
    comp_choice = random.choice(choices)
    print(comp_choice)



    if (player_choice == "Rock") and (comp_choice == "Paper"):
        print("Computer Won!")
        computer_winnings += 1
    if (player_choice == "Paper") and (comp_choice == "Paper"):
        print("Tie.")
    if (player_choice == "Scissor") and (comp_choice == "Paper"):
        print("You Won!")
        player_winnings += 1

    if (player_choice == "Scissors") and (comp_choice == "Rock"):
        print("Computer Won!")
        computer_winnings += 1
    if (player_choice == "Rock") and (comp_choice == "Rock"):
        print("Tie.")
    if (player_choice == "Paper") and (comp_choice == "Rock"):
        print("You Won!")
        player_winnings += 1

    if (player_choice == "Paper") and (comp_choice == "Scissors"):
        print("Computer Won!")
        computer_winnings += 1
    if (player_choice == "Scissors") and (comp_choice == "Scissors"):
        print("Tie.")
    if (player_choice == "Rock") and (comp_choice == "Scissors"):
        print("You Won!")
        player_winnings += 1

    if computer_winnings > player_winnings:
        winner = computer
    elif player_winnings > computer_winnings:
        winner = player
    player_winnings = str(player_winnings)
    computer_winnings = str(computer_winnings)

    exit_choice = input("Do you want to continue. If yes enter Y or y. If no enter N or n. Caution : If you press n or N then your progress will be lost.")
    if exit_choice == 'n' or 'N':
        print("Your winnings are = " + player_winnings)
        print("Computer winnings are = " + computer_winnings)
        print("The winner of this match is" + winner)
        break
    elif exit_choice == 'Y' or exit_choice == 'y':
        continue
Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
Ved Asish
  • 1
  • 1

2 Answers2

1

I think when computer_winnings == player_winnings, then winner is not defined.
Thus, initialize winner as None before if computer_winnings > player_winnings:

winner = None
Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11
1

You are pointing the variable winner to variables because you are not using strings when you say winner = computer or winner = player.

Both computer and player are strings. As such, winner should be defined as winner = 'computer' or winner = 'player'.

franciscofcosta
  • 833
  • 5
  • 25
  • 53