Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner question: returning a boolean value from a function in Python
    text
    copied!<p>I'm trying to get this rock paper scissors game to either return a Boolean value, as in set <code>player_wins</code> to True or False, depending on if the player wins, or to refactor this code entirely so that it doesn't use a while loop. I'm coming from the sysadmin side of the world, so please be gentle if this is written in the wrong style. I have tried a few things, and I understand TIMTOWTDI, and would just like some input. </p> <p>Thanks.</p> <pre><code>import random global player_wins player_wins=None def rps(): player_score = 0 cpu_score = 0 while player_score &lt; 3 and cpu_score &lt; 3: WEAPONS = 'Rock', 'Paper', 'Scissors' for i in range(0, 3): print "%d %s" % (i + 1, WEAPONS[i]) player = int(input ("Choose from 1-3: ")) - 1 cpu = random.choice(range(0, 3)) print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu]) if cpu != player: if (player - cpu) % 3 &lt; (cpu - player) % 3: player_score += 1 print "Player wins %d games\n" % player_score else: cpu_score += 1 print "CPU wins %d games\n" % cpu_score else: print "tie!\n" rps() </code></pre> <p>I'm trying to do something like this:</p> <pre><code> print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu]) if cpu != player: if (player - cpu) % 3 &lt; (cpu - player) % 3: player_score += 1 print "Player wins %d games\n" % player_score if player_score == 3: return player_wins==True else: cpu_score += 1 print "CPU wins %d games\n" % cpu_score if cpu_score == 3: return player_wins==False else: print "tie!\n" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload