Reputation: 23
import random
rps = ['Rock', 'Paper', 'Scissor']
diction = {'R': 'Rock', 'P': 'Paper', 'S': 'Scissor'}
human = 0
PC = 0
print('R : Rock \n P : Paper \n S : Scissor')
computer = random.choice(rps)
player = input().capitalize()
choice = diction[player]
print(computer, ' vs. ', choice)
# Check for a tie
def check_tie():
# Checks if it's a tie
if computer == choice:
global human
global PC
print('computer = ', PC, 'you = ', human)
return
# Check for a win
def check_win():
check_rock_win()
check_paper_win()
check_scissor_win()
return
# Check if rock wins
def check_rock_win():
if computer == 'Rock' and choice == 'Scissor':
global human
global PC
human = human + 0
PC = PC + 1
print('computer = ', PC, 'you = ', human)
elif computer == 'Scissor' and choice == 'Rock':
global human
global PC
human = human + 1
PC = PC + 0
print('computer = ', PC, 'you = ', human)
return
# check if paper wins
def check_paper_win():
if computer == 'Rock' and choice == 'Paper':
global human
global PC
human = human + 1
PC = PC + 0
print('computer = ', PC, 'you = ', human)
elif computer == 'Paper' and choice == 'Rock':
global human
global PC
human = human + 0
PC = PC + 1
print('computer = ', PC, 'you = ', human)
return
# check if scissor wins
def check_scissor_win():
if computer == 'Scissor' and choice == 'Paper':
global human
global PC
human = human + 0
PC = PC + 1
print('computer = ', PC, 'you = ', human)
elif computer == 'Paper' and choice == 'Scissor':
global human
global PC
human = human + 1
PC = PC + 0
print('computer = ', PC, 'you = ', human)
return
Here I'm trying to make a simple Rock,Paper,scissor game, in the function check_rock_win in the elif loop, it is giving an error that variable 'human' is used prior to global declaration, although I have declared it first hand.
P.S - I'm still new to Python!
Upvotes: 0
Views: 52
Reputation: 2709
In all your functions, place the global
statements outside the if
statements, otherwise, it is not always executed, for example,
def check_tie():
global human
global PC
# Checks if it's a tie
if computer == choice:
print('computer = ', PC, 'you = ', human)
return
Upvotes: 1