[python] Beginner question: returning a boolean value from a function in Python

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"

This question is related to python function boolean

The answer is


Have your tried using the 'return' keyword?

def rps():
    return True

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to boolean

Convert string to boolean in C# In c, in bool, true == 1 and false == 0? Syntax for an If statement using a boolean Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Ruby: How to convert a string to boolean Casting int to bool in C/C++ Radio Buttons ng-checked with ng-model How to compare Boolean? Convert True/False value read from file to boolean Logical operators for boolean indexing in Pandas