Reputation: 17
So the problem I'm having is my counter keeps reseting when it try to add up the figures I tried figuring out a way to make the counter portion into a function but I couldnt figure it out
winP1=0
winP2=0
tie=0
ans = input('Would you like to play ROCK, PAPER, SCISSORS?: ')
while ans == 'y':
p1c = input('Player 1 enter either R,P, or S: ') #p1c stands for "player 1 choice"
p2c = input('Player 2 enter either R,P, or S: ')
ans = input('Would you like to play again: ')
def game(p1c,p2c):
if p1c == p2c: #if its a tie we are going to add to the Tie variable
return 0
elif p1c == 'R' and p2c == 'P': #We will only set the Player 1 wins because
return 1 #player 2 wins can be set as else
elif p1c == 'P' and p2c == 'S':
return 1
elif p1c == 'S' and p2c == 'R':
return 1
else:
return -1
result = game(p1c,p2c)
if result == -1:
winP2 += 1
if result == 0:
tie += 1
else:
winP1 +=1
print('Player 1 won {} times. \nPlayer 2 won {} times. \nThere were {} ties.'.format(winP1,winP2,tie))
Upvotes: 1
Views: 69
Reputation: 8492
Welcome to StackOverflow and to Python programming! I hope you have a great time here.
Your counter keeps resetting because, to the system, you're only playing one game:
while ans == 'y':
p1c = input('Player 1 enter either R,P, or S: ') #p1c stands for "player 1 choice"
p2c = input('Player 2 enter either R,P, or S: ')
ans = input('Would you like to play again: ')
I'm going to move some things around in your code to get you the result you want, editing minimally, and hopefully that'll show you what you need to do. You're on the right track!
We'll use a dictionary called state
to keep track of the game state and pass it around.
state = {
'winP1':0,
'winP2':0,
'tie':0
}
def game(p1c,p2c):
if p1c == p2c: #if its a tie we are going to add to the Tie variable
return 0
elif p1c == 'R' and p2c == 'P': #We will only set the Player 1 wins because
return 1 #player 2 wins can be set as else
elif p1c == 'P' and p2c == 'S':
return 1
elif p1c == 'S' and p2c == 'R':
return 1
else:
return -1
def process_game(p1c, p2c, state): # Move this into a function
result = game(p1c,p2c)
if result == -1:
state['winP2'] += 1
if result == 0:
state['tie'] += 1
else:
state['winP1'] +=1
while ans == 'y':
p1c = input('Player 1 enter either R,P, or S: ') #p1c stands for "player 1 choice"
p2c = input('Player 2 enter either R,P, or S: ')
process_game(p1c, p2c, state)
ans = input('Would you like to play again: ')
print('Player 1 won {} times. \nPlayer 2 won {} times. \nThere were {} ties.'.format(state['winP1'],state['winP2'],state['tie']))
Upvotes: 3