I'm trying to get this rock paper scissors game to either return a Boolean value, as in set player_wins
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.
Thanks.
import random
global player_wins
player_wins=None
def rps():
player_score = 0
cpu_score = 0
while player_score < 3 and cpu_score < 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 < (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()
I'm trying to do something like this:
print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu])
if cpu != player:
if (player - cpu) % 3 < (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"
bool() in Python Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.
The Python return statement is a special statement that you can use inside a function or method to send the function's result back to the caller. A return statement consists of the return keyword followed by an optional return value. The return value of a Python function can be any Python object.
A Boolean function is like a built-in function except that it returns a value of true or false instead of number, string, or date. The result of a Boolean function cannot be printed; it can only be used as a condition.
Boolean comparisons are used to test the operands and to return boolean values. Boolean expressions can belong to any other types of 4gl expressions. For example, you can use INT and SMALLINT to store the value returned by a Boolean expression.
Ignoring the refactoring issues, you need to understand functions and return values. You don't need a global at all. Ever. You can do this:
def rps():
# Code to determine if player wins
if player_wins:
return True
return False
Then, just assign a value to the variable outside this function like so:
player_wins = rps()
It will be assigned the return value (either True or False) of the function you just called.
After the comments, I decided to add that idiomatically, this would be better expressed thus:
def rps():
# Code to determine if player wins, assigning a boolean value (True or False)
# to the variable player_wins.
return player_wins
pw = rps()
This assigns the boolean value of player_wins
(inside the function) to the pw
variable outside the function.
Have your tried using the 'return' keyword?
def rps():
return True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With