初学者的问题:从Python中的函数返回一个布尔值 [英] Beginner question: returning a boolean value from a function in Python

查看:3143
本文介绍了初学者的问题:从Python中的函数返回一个布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个剪刀石头布的游戏要么返回一个布尔值,如集 player_wins 来真或假,这取决于如果玩家获胜,或完全重构这个code,以便它不使用whil​​e循环。
我是从世界的系统管理员那边传来,如果这是写在错误的风格,所以请温柔。
我已经尝试了一些事情,我理解TIMTOWTDI,而只是想一些投入。

感谢。

 进口随机全球player_wins
player_wins =无高清RPS():    player_score = 0
    cpu_score = 0    而player_score< 3 cpu_score< 3:        武器='摇滚','纸','剪刀'        对于i在范围(0,3):
          打印%D%的%(I + 1,武器[I])        玩家= INT(输入(选择1-3)) - 1
        CPU = random.choice(范围(0,3))        打印%s和%的%(武器[播放]武器[CPU])
        如果CPU =玩家!
          如果(球员 - CPU)%,第3版; (CPU - 玩家)%3:
            player_score + = 1
            打印玩家赢得%d个游戏\\ n%player_score
          其他:
            cpu_score + = 1
            打印CPU赢得%d个游戏\\ n%cpu_score
        其他:
          打印扎!\\ n
RPS()

我试图做这样的事情:

 打印%s和%的%(武器[播放]武器[CPU])
    如果CPU =玩家!
      如果(球员 - CPU)%,第3版; (CPU - 玩家)%3:
        player_score + = 1
        打印玩家赢得%d个游戏\\ n%player_score
        如果player_score == 3:
            返回player_wins ==真
      其他:
        cpu_score + = 1
        打印CPU赢得%d个游戏\\ n%cpu_score
        如果cpu_score == 3:
            返回player_wins ==假
    其他:
      打印扎!\\ n


解决方案

忽略的重构问题,你需要了解的功能和返回值。你并不需要一个全球性的。永远。你可以这样做:

  DEF RPS():
    #code,以确定是否玩家获胜
    如果player_wins:
        返回True    返回False

然后,只需赋值给变量这一功能外,像这样:

  player_wins = RPS()

这将您刚才调用的函数指定返回值(True或False)。


的意见后,我决定要补充的是习惯用法,这将是更好的EX从而pressed:

  DEF RPS():
     #code,以确定是否玩家赢,分配一个布尔值(TRUE或FALSE)
     #给变量player_wins。     返回player_wins PW = RPS()

这赋予 player_wins 的布尔值(在函数内部)的 PW 变量之外的功能。

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"

解决方案

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.

这篇关于初学者的问题:从Python中的函数返回一个布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆