1
votes

I want to write a function simul() that takes an integer and simulates n rounds of Rock,Paper,Scissors between Player1 and Player2. The player who wins the most rounds wins the n-round game, with ties possible.

Example:

simul(1) Player1

simul(1) Tie

simul(100) Player2

I've written:

def rps():   
 lst['R', 'P', 'S']
 p1=random.choice(lst)
 p2=random.choice(lst)
 if p1+p2=='RS' or p1+p2=='PR' or p1+p2=='SP':
      return '-1'
 elif p1+p2=='RP' or p1+p2=='PS' or p1+p2=='SR':
      return '1'
 else:
      return '0'

def simul(n):
 total=0
 for i in range(n):
      if rps()==-1:
           total+=-1
      elif rps()==1:
           total+=1
      else:
           total+=0
 if total<0:
      print('Player1')
 elif total==0:
      print('Tie')
 else:
      print('Player2')

When I run the simul() code, however, the only result I ever get is 'Tie'. How can I fix it??

2
Not a duplicate. The only thing the two questions have in common is Rock Paper Scissors. - user2357112 supports Monica
Start by adding some debugging lines. Where is your flow going, what does the rps call do, how many runs are you doing, why is it always returning tie: is it because rps() always returns '0' or is it because some other thing, etc etc etc. - Nanne

2 Answers

2
votes

In the function rps() you are returning strings, but in the function simul() you are comparing the result of rps() with integers.

Since you are using -1, 0 and 1, you should return integers instead.

def rps():   
    lst['R', 'P', 'S']
    p1=random.choice(lst)
    p2=random.choice(lst)
    if p1+p2 == 'RS' or p1+p2 == 'PR' or p1+p2 == 'SP':
        return -1
    elif p1+p2 == 'RP' or p1+p2 == 'PS' or p1+p2 == 'SR':
        return 1
    else:
        return 0

Note: Python use 'single quotes', "double quotes" and """triple quotes""" to recognize strings.

2
votes

You have two problems. The first is that you're returning strings:

if p1+p2=='RS' or p1+p2=='PR' or p1+p2=='SP':
    return '-1'
elif p1+p2=='RP' or p1+p2=='PS' or p1+p2=='SR':
    return '1'
else:
    return '0'

but comparing the result with integers:

if rps()==-1:
    total+=-1
elif rps()==1:
    total+=1
else:
    total+=0

An integer is not considered equal to its textual representation. Return integers from rps instead of strings.

The second problem is that in simul, if the rps()==-1 branch isn't taken, you simulate another round of play for the second check:

if rps()==-1:
    total+=-1
elif rps()==1:
    total+=1

Simulate one round per iteration of the loop, and use the result of that one round in both checks:

for i in range(n):
    result = rps()
    if result == -1:
        total += -1
    elif result == 1:
        total += 1
    else:
        total += 0