简体   繁体   English

(python) 具有变量 arguments 的函数

[英](python) functions with variable arguments

How do I get the following code to work so the functions are executed on the player scores separately and allow them to accumulate?(btw, I'm very new to python so don't judge)如何使以下代码工作,以便分别对玩家分数执行函数并允许它们累积?(顺便说一句,我对 python 很陌生,所以不要判断)

player_1_score=0
player_2_score=0
def six(score):
    print('you rolled a 6')
    print('''
 -------
| o   o |
| o   o |
| o   o |
 -------   ''')
    score+=6

def five(score):
     print('you rolled a 5')
     print('''
 -------
| o   o |
|   o   |
| o   o |
 -------    ''')
     score+=5

#for player1
five(player_1_score)
six(player_1_score)
print('p1 score:',int(player_1_score))

#for player2
five(player_2_score)
six(player_2_score)
print('p2 score:',int(player_1_score))

the code should display: 11 for player scores when run but displays 0 instead.代码应显示:运行时玩家得分为 11,但改为显示 0。

You need to return the new score from each function:您需要从每个 function 返回新分数:

def six(score):
    print('you rolled a 6')
    print('''
 -------
| o   o |
| o   o |
| o   o |
 -------   ''')
    score += 6
    return score

But the call sites need to be like this:但是呼叫站点需要是这样的:

player_1_score = five(player_1_score)

Compulsory link to names by Ned Batchelder. Ned Batchelder 的名字的强制性链接。

You could update the scores separately, ie not in the "roll dice" functions.您可以单独更新分数,即不在“掷骰子”功能中。 I think it is more readable in this phase.我认为在这个阶段它更具可读性。 Later, when you advance in learning Python, you will probably solve the problem differently, perhaps with a Player class and a mapping (number 1 to 6) --> (dice face as a string).稍后,当您继续学习 Python 时,您可能会以不同的方式解决问题,也许使用Player class 和映射(数字 1 到 6)-->(骰子面作为字符串)。

def six():
    print('you rolled a 6')
    print('''\
 -------
| o   o |
| o   o |
| o   o |
 -------   ''')
    return 6

def five():
    print('you rolled a 5')
    print('''\
 -------
| o   o |
|   o   |
| o   o |
 -------    ''')
    return 5

player_1_score = 0
player_2_score = 0

#for player1
player_1_score += five()
player_1_score += six()

#for player2
player_2_score += five() + six()

print('scores:', player_1_score, player_2_score)

Note: I added a continuation backslash to the print('''\ ) statement to get rid of the blank line.注意:我在print('''\ ) 语句中添加了一个连续的反斜杠来删除空行。 I like it better but you may prefer the blank line.我更喜欢它,但您可能更喜欢空白行。

You want to change player's score but you are not modifying it either.你想改变玩家的分数,但你也没有修改它。 You are sending player's score but if you change score in your functions ( six and five ), you are not modifying original score.您正在发送玩家的分数,但如果您在函数( six five )中更改分数,则不会修改原始分数。 If you want to modify original score, you can return score from your functions and assign it to your player's scores.如果要修改原始分数,可以从函数中返回score并将其分配给玩家的分数。 For example:例如:

def five(score):
     print('you rolled a 5')
     print('''
 -------
| o   o |
|   o   |
| o   o |
 -------    ''')
     score+=5
     return score

#for player1
player_1_score=five(player_1_score)
print('p1 score:',int(player_1_score))

Passing an argument to the score parameter for five(score) and six(score) creates a score variable within the scope of each function.将参数传递给five(score) score six(score)的分数参数会在每个 function 的 scope 内创建一个score变量。 So when the program calls five(player_1_score) , the value of player_1_score is copied to score .因此,当程序调用five(player_1_score)时, player_1_score的值被复制到score中。 You'll need to你需要

  1. Modify each of your functions to return the score value修改您的每个函数以返回score

  2. Assign the output of each function to each outer scope variable将每个 function 的 output 分配给每个外部 scope 变量

player_1_score=0
player_2_score=0
def six(score):
    print('you rolled a 6')
    print('''
 -------
| o   o |
| o   o |
| o   o |
 -------   ''')
    score+=6
    return score

def five(score):
     print('you rolled a 5')
     print('''
 -------
| o   o |
|   o   |
| o   o |
 -------    ''')
     score += 5
     return score


#for player1
player_1_score = five(player_1_score)
player_1_score = six(player_1_score)
print('p1 score:',int(player_1_score))

#for player2
player_2_score = five(player_2_score)
player_2_score = six(player_2_score)
print('p2 score:',int(player_2_score))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM