两人骰子游戏得分问题

时间:2019-01-05 11:20:05

标签: python python-3.x dice

这是一个为两个用户掷出2次骰子5次的游戏。如果骰子总数相等,则玩家获得10分;如果很奇怪,他们输5。

int

与此不对的是,假设用户掷出1和2,它们加起来等于3,这是一个奇数,游戏会从3中减去-5,使总数变成-2,但我不想要变成负数,我希望它是这样,如果他们确实得到负分,那就说他们得到了0分而不是负分

2 个答案:

答案 0 :(得分:1)

减去玩家的积分后,您可以使用max():

playerOnePoints = playerOnePoints - 5
playerOnePoints = max(0, playerOnePoints)

如果playerOnePoints为负,则为0;如果playerOnePoints为正,则为您。

此外,您也可以使用abs()来做到这一点。

def x(number):
    return (abs(number)+number)/2

x(-2) # This would return 0
x(2) # This would return 2

答案 1 :(得分:1)

PSM答案的另一种选择是使用if语句以简化操作:

playerOnePoints = playerOnePoints - 5
if playerOnePoints < 0:
    playerOnePoints = 0

或更少的行,可能不那么简单:

# playerOnePoints = playerOnePoints - 5   # Delete this line and just use below
if playerOnePoints >= 5:
    playerOnePoints = playerOnePoints - 5