0

I am trying to make a simple RPS game and can't see what I am doing wrong. I declase pc_choise, player_choice and turn as global variables but I can't modify them in functions such as checkWinner().

If I print that value after the function has been called, it still has the initial value.

Code:

    import random
    import sys

    pc_choices = ['r','p','s']
    pc_choise = ''
    player_choice = ''
    turns = 0

    print("\t\tWelcome to Rock Paper Scissors")


    def getPcChoice():
      return random.randint(1,3) - 1

    def getUserChoice():

    player_choice = input('Please choose: ')
    turns = 1

    if(player_choice.lower() not in pc_choices):
      print('\nPlease use R, P, or S -  *not case sensitive*\n')
      getUserChoice()
    else:
      pc_choise = pc_choices[getPcChoice()]
      print('\nYou picked ' + player_choice + ' and the PC picked ' + 
      pc_choise)
      checkWinner()

    def checkWinner():



      if(player_choice.lower() == pc_choise.lower()):
        print('Tie')
      elif(player_choice.lower() == 'r' and pc_choise.lower() == 'p'
        or   player_choice.lower() == 'p' and pc_choise.lower() == 's'
        or   player_choice.lower() == 's' and pc_choise.lower() == 'r'):
        print('You win! ')
      else:
        print('You lose! ')


    getUserChoice()
Vladut Maican
  • 41
  • 1
  • 5
  • If you want to modify a global variable within a function, you have to add `global player_choice` and `global pc_choise` to the top of the function. – mario_sunny Nov 04 '19 at 13:55
  • Or even better- refactor your code to avoid global variables altogether. [Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – mario_sunny Nov 04 '19 at 13:55
  • `getUserChoice` could easily be modified to just return the choice instead of modifying the global. – Carcigenicate Nov 04 '19 at 14:11
  • @mario_sunny what should I be looking at when refactoring? Some major steps to follow? – Vladut Maican Nov 04 '19 at 14:11
  • @VladutMaican If you post this code on [Code Review](https://codereview.stackexchange.com/questions) as complete, runnable code, I or someone else can go over it and help improve it. – Carcigenicate Nov 04 '19 at 16:31

2 Answers2

2

add the following code in the first line of the code:

global pc_choices and global pc_choice etc.

this is to mark the variable will use is a global variable.

0

You need to declare the variables as global within the function scope so that Python interpreter can interpret them accordingly, otherwise, they get the function scope by default. I also found indentation issues which I have fixed. Below is your modified code:

import random
import sys

pc_choices = ['r','p','s']

pc_choise = ''
player_choice = ''
turns = 0

print("\t\tWelcome to Rock Paper Scissors")

def getPcChoice():
    return random.randint(1,3) - 1

def getUserChoice():
    global pc_choise
    global player_choice

    player_choice = input('Please choose: ')
    turns = 1

    if(player_choice.lower() not in pc_choices):
        print('\nPlease use R, P, or S -  *not case sensitive*\n')
        getUserChoice()
    else:
        pc_choise = pc_choices[getPcChoice()]
        print('\nYou picked ' + player_choice + ' and the PC picked ' + pc_choise)
        checkWinner()

def checkWinner():
    global pc_choise
    global player_choice

    if(player_choice.lower() == pc_choise.lower()):
        print('Tie')
    elif(player_choice.lower() == 'r' and pc_choise.lower() == 'p'
        or   player_choice.lower() == 'p' and pc_choise.lower() == 's'
        or   player_choice.lower() == 's' and pc_choise.lower() == 'r'):
        print('You win! ')
    else:
        print('You lose! ')

getUserChoice()

However, as suggested by @mario_sunny in the comments, it's better to avoid using global variables.

Zeeshan
  • 1,078
  • 9
  • 14