Python 冰冻骰子滚了?
我正在创造一个游戏,沿著掷骰子游戏的路线。我必须给用户掷5个骰子,并询问他们希望重新掷哪个骰子的5位数。例如:Python 冰冻骰子滚了?,python,python-3.x,if-statement,random,dice,Python,Python 3.x,If Statement,Random,Dice,我正在创造一个游戏,沿著掷骰子游戏的路线。我必须给用户掷5个骰子,并询问他们希望重新掷哪个骰子的5位数。例如: Your roll is: 5 1 5 5 1 Which dice should I roll again?: 234 Your new roll is: 5 7 2 4 1 中间的三个数字会发生变化,因为这是掷骰子的结果。 我不知道如何有效地做到这一点。我可以创建240个if语句,但这似乎不是正确的方法 这是我迄今为止的代码: import random def yahtze
Your roll is: 5 1 5 5 1
Which dice should I roll again?: 234
Your new roll is: 5 7 2 4 1
中间的三个数字会发生变化,因为这是掷骰子的结果。
我不知道如何有效地做到这一点。我可以创建240个if语句,但这似乎不是正确的方法
这是我迄今为止的代码:
import random
def yahtzee():
dice1 = random.randrange(1,6)
dice2 = random.randrange(1,6)
dice3 = random.randrange(1,6)
dice4 = random.randrange(1,6)
dice5 = random.randrange(1,6)
print('Your roll is: ' + ' ' + str(dice1) + ' ' + str(dice2) + ' ' + str(dice3) + ' ' + str(dice4) + ' ' + str(dice5))
reroll = input('Which dice should I roll again?: ')
这给了我一个结果:
yahtzee()
Your roll is: 4 3 2 1 5
Which dice should I roll again?:
不知道该如何重新开始。任何帮助都将不胜感激!谢谢大家! 一般来说,管理存储在列表中的结果要容易得多:
def yahtzee():
dice = [random.randrange(1, 6) for _ in range(5)]
print('Your roll is: ', *dice)
reroll = input('Which dice should I roll again?: ')
for i in reroll:
dice[int(i) - 1] = random.randrange(1, 6)
print('Your roll is: ', *dice)
示例输出:
你的名单是:5 3 2 5 3
我应该再掷哪个骰子?:12
你的名单是:1 2 5 3
啊,太棒了,非常感谢,我觉得我的代码效率很低,非常感谢!