如何将函数中的输出数据重新输入函数

时间:2017-11-20 12:46:19

标签: python

def play(player_1_skill, player_1_strength,player_2_skill,player_2_strength,divider,skillmod,strengthmod):
    strengthmod = math.floor((abs(player_1_strength - player_2_strength))/divider)
    skillmod = math.floor((abs(player_1_skill - player_2_skill))/divider)
    die_p1 = r.randint(1,6)
    die_p2 = r.randint(1,6)
    if player_1_strength > player_2_strength:
        print("Player 1 Wins")
        player_1_strength = strengthmod + player_1_strength
        player_1_skill = skillmod + player_1_skill
        player_2_strength = player_2_strength - strengthmod
        player_2_skill = player_2_skill - skillmod
    elif player_2_strength > player_1_strength:
        print("Player 2 Wins")
        player_2_strength = strengthmod + player_2_strength
        player_2_skill = skillmod + player_2_skill
        player_1_strength = player_1_strength - strengthmod
        player_1_skill = player_1_skill - skillmod
    else:
        print("Tie")

    print("The skill modifier : "+ str(skillmod))
    print("The strength modifier: " + str(strengthmod))
    print("Player 1, Your strength is: " + str(player_1_strength))
    print("Player 1, Your skill is: " + str(player_1_skill))
    print("Player 2, Your strength is: " + str(player_2_strength))
    print("Player 2 , Your skill is: " + str(player_2_skill))





    return (player_1_strength, player_1_skill, player_2_strength, player_2_skill,skillmod,strengthmod)



play(player_1_skill, player_1_strength,player_2_skill, player_2_strength,divider,skillmod,strengthmod)
play(player_1_skill, player_1_strength,player_2_skill, player_2_strength,divider,skillmod,strengthmod)

如何使用同一功能中的不同数据重复功能 例如,功能输出数据,输出数据返回功能

1 个答案:

答案 0 :(得分:1)

最简单的方法是将这些数据放入或词典中。

字典示例:

def play(player_1, player_2, divider):
    # access the individual values like:
    player_1['strength'] = ...something...
    ...something... = player_2['skill'] / divider

# then

player_1 = {
    'strength': 10,
    'skill': 20,
}
player_2 = {
    'strength': 15,
    'skill': 16,
}

play(player_1, player_2)

现在函数play可以修改你传递的字典中的值。