Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:20:20+00:00 2026-05-30T16:20:20+00:00

The main problem I’m having is trying to get the total wins loses and

  • 0

The main problem I’m having is trying to get the total wins loses and ties when user is playing with computer(random function). But I keep getting this error whenever I’m inputting either 1, 2, or 3 for rock paper scissors for player_choice.

Welcome to a game of paper, rock, scissors!
Please input the correct number according 
to the object.
Select rock(1), paper(2), or scissors(3): 2
Computer chose ROCK .
You chose PAPER .
Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line    
114, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line   
43, in main
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 
106, in determine_winner
builtins.UnboundLocalError: local variable 'win' referenced before assignment

Clearly its a local variable issue. Are there any other solutions?
Here is my code:

#import the library function "random" so that you can use it for computer
#choice
import random

#define main
def main():
    #assign win, lose, and tie variables to zero so that later it can be added
    #and displayed
    win = 0
    lose = 0
    tie = 0

    #control loop with 'y' variable
    play_again = 'y'

    #start the game
    while play_again == 'y':
        #make a welcome message and give directions
        print('Welcome to a game of paper, rock, scissors!')
        print('Please input the correct number according')
        print('to the object.')

        #write computer and players choices as value returning functions and
        #assign them to variables
        computer_choice = get_computer_choice()
        player_choice = get_player_choice()

        #print outcome
        print('Computer chose', computer_choice, '.')
        print('You chose', player_choice, '.')

        #determine who won by defining a function
        determine_winner(computer_choice, player_choice)

        #ask the user if they want to play again
        play_again = input("Play again? Enter 'y' for yes or 'n' for no. ")

    #print results
    print('Your total wins are', win, '.')
    print('Your total losses are', lose, '.')
    print('Your total ties are', tie, '.')

#define computer choice
def get_computer_choice():
    #use imported random function from library
    choice = random.randint(1,3)

    #assign what the computer chose to rock, paper, or scissors
    if choice == 1:
        choice = 'ROCK'
    elif choice == 2:
        choice = 'PAPER'
    else:
        choice = 'SCISSORS'

    #return value
    return choice

#define player choice
def get_player_choice():
    #assign input to variable by prompting user
    choice = int(input("Select rock(1), paper(2), or scissors(3): "))

    #use while function if user inputs the invalid selection
    while choice != 1 and choice != 2 and choice != 3:
        print('The valid numbers are rock(type in 1), paper(type in 2),')
        print('or scissors(type in 3).')
        choice = int(input('Enter a valid number please: '))

    #assign what the player chose to rock, paper, or scissors
    if choice == 1:
        choice = 'ROCK'
    elif choice == 2:
        choice = 'PAPER'
    else:
        choice = 'SCISSORS'

    #return value
    return choice

#determine the winner by assigning the assigned variables
def determine_winner(computer_choice, player_choice):
    #if its a tie, add 1 to tie variable and display message
    if computer_choice == player_choice:
        tie += 1
        print("It's a tie!")

    #if its a win, add to win variable and display message
    elif computer_choice == 'SCISSORS' and player_choice == 'ROCK':
        win += 1
        print('ROCK crushes SCISSORS! You win!')
    elif computer_choice == 'PAPER' and player_choice == 'SCISSORS':
        win += 1
        print('SCISSORS cut PAPER! You win!')
    elif computer_choice == 'ROCK' and player_choice == 'PAPER':
        win += 1
        print('PAPER covers ROCK! You win!')

    #if it does not match any of the win criteria then add 1 to lose and
    #display lose message
    else:
        lose += 1
        print('You lose!')
main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:20:21+00:00Added an answer on May 30, 2026 at 4:20 pm

    The function determine_winner() can’t see the variables win, tie and lose that you have defined in main(). Therefore you can’t do win +=1.

    You usually don’t use a main() routine in Python anyway (I’ve seen a few questions here recently that use them – whoever is teaching this?), but even if you move its contents into the top level of the program, it wouldn’t work because win += 1 would still fail for the same reason.

    You could define local variables win, tie and lose in determine_winner() and have it return their values, then add those to the respective variables in the top level code. In fact, you don’t even need those variables in that function at all.

    For example:

    def determine_winner(computer_choice, player_choice):
        #if its a tie, add 1 to tie variable and display message
        if computer_choice == player_choice:
            print("It's a tie!")
            return 0
    
        #if its a win, add to win variable and display message
        elif computer_choice == 'SCISSORS' and player_choice == 'ROCK':
            print('ROCK crushes SCISSORS! You win!')
            return 1
        elif computer_choice == 'PAPER' and player_choice == 'SCISSORS':
            print('SCISSORS cut PAPER! You win!')
            return 1
        elif computer_choice == 'ROCK' and player_choice == 'PAPER':
            print('PAPER covers ROCK! You win!')
            return 1
    
        #if it does not match any of the win criteria then add 1 to lose and
        #display lose message
        else:
            print('You lose!')
            return -1
    

    and in the top level:

    result = determine_winner(computer_choice, player_choice)
    if result == -1:
        lose += 1
    elif result == 0:
        tie += 1
    else:
        win += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Main Problem : Get rid of # when user clicks on anchor tag. I
I'm trying to learn the MVVM pattern. The main problem I'm having is learning
The main problem here is to evaluate the user function at some point because
The main problem I'm having is pulling data from tables, but any other general
My main problem is I don't understand how to get the position of the
Problem description : - Step 1: Take input FILE_NAME from user at main thread.
The main problem of this question is when we pass T into some function
My main problem is trying to find a suitable solution to automatically turning this,
I'm still with ndk-gdb, now trying to solve the main problem that leaded me
I'm having some issues with parsing CSV data with quotes. My main problem is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.