初学者问题:从 Python 中的函数返回一个布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4165933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:35:05  来源:igfitidea点击:

Beginner question: returning a boolean value from a function in Python

pythonfunctionboolean

提问by matt

I'm trying to get this rock paper scissors game to either return a Boolean value, as in set player_winsto 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.

我试图让这个石头剪刀布游戏返回一个布尔值,如设置player_wins为 True 或 False,取决于玩家是否获胜,或者完全重构此代码,使其不使用 while 循环。我来自世界的系统管理员方面,所以如果以错误的方式编写,请保持温和。我已经尝试了一些东西,我了解 TIMTOWTDI,并且想要一些输入。

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"

采纳答案by asthasr

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.

它将被分配您刚刚调用的函数的返回值(True 或 False)。



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 pwvariable outside the function.

这将player_wins(函数内部)的布尔值分配给函数pw外部的变量。

回答by Cpt. eMco

Have your tried using the 'return' keyword?

您是否尝试过使用“return”关键字?

def rps():
    return True