当持有两个整数骰子时,Python偶然出现列表索引错误
当我选择持有两个骰子时,它并不总是遵循脚本。它发生在大约第50行。有时它甚至不打印列表。 (作为旁注——如果有人能简化代码,我们将不胜感激。) 任何人都能为这个问题提供解决方案和理由吗。 (脚本如下)当持有两个整数骰子时,Python偶然出现列表索引错误,python,python-3.x,Python,Python 3.x,当我选择持有两个骰子时,它并不总是遵循脚本。它发生在大约第50行。有时它甚至不打印列表。 (作为旁注——如果有人能简化代码,我们将不胜感激。) 任何人都能为这个问题提供解决方案和理由吗。 (脚本如下) 似乎您最好阅读一下列表和随机模块提供的内容 下面是生成一轮Yatzy的建议简单解决方案 注意:该解决方案使用格式化字符串文本,因此需要Python3.6或更高版本 #! /usr/bin/env python3 import sys import random greeting = """We
似乎您最好阅读一下
列表
和随机
模块提供的内容
下面是生成一轮Yatzy的建议简单解决方案
注意:该解决方案使用格式化字符串文本,因此需要Python3.6或更高版本
#! /usr/bin/env python3
import sys
import random
greeting = """Welcome to Yatzy
Where there are closed questions answer lower case with 'y' or n'
When asked which dice to hold answer with a list of dice separated by
space.
At most 3 rounds per turn. Between each turn decide what numbers to
keep before rolling again.
Please be aware that this a variation of the traditional game. Should
you hold a number- you cannot 'unhold' it. It has been added to your
final roll for the round.
Do not be tempted to hold onto a number you haven't rolled - this will
not work and won't be added to your final score.
"""
# Assume we have N dice. There are 3 rounds per turn and between
# rounds the player chooses r numbers to keep. The next round will
# then have N - r dice to roll. After each turn the kept dice
# collection is displayed.
#
# The turn stops when 3 rounds have been played or when there are no
# more dice to roll or when the player decides to stop.
def play_one_turn(dice=5, turns=3):
keep = []
msg = "Dice to hold: "
while turns and dice:
turns -= 1
print(f"Kept: {keep}")
play = random.choices(range(1, 7), k=dice)
print(f"Throw --> {play}")
for h in map(int, input(msg).split()):
if h in play:
keep.append(h)
dice -= 1
play.remove(h)
ask = "Another round? (yes/no) "
if not input(ask).strip().upper().startswith('Y'):
break
return keep
if __name__ == "__main__":
print(greeting)
score = play_one_turn()
print(f"Play result {score}")
你拿两个骰子是什么意思?我刚刚运行了you程序,当我不断按enter键时,它打印了一个包含5个随机数的列表。该程序代表一个yahtzee游戏:你掷5次骰子,然后你就可以拿骰子了。不幸的是,我还没来得及继续,错误就来了。很明显,你可以持有任意数量的五个骰子,但我只抽出时间对持有的两个骰子的结果进行编码。目前你只能掷一次。那么,在你决定要持有多少骰子之后,会发生什么呢?非常感谢!这回答了我所有的问题,并在足够简单的代码理解!
#! /usr/bin/env python3
import sys
import random
greeting = """Welcome to Yatzy
Where there are closed questions answer lower case with 'y' or n'
When asked which dice to hold answer with a list of dice separated by
space.
At most 3 rounds per turn. Between each turn decide what numbers to
keep before rolling again.
Please be aware that this a variation of the traditional game. Should
you hold a number- you cannot 'unhold' it. It has been added to your
final roll for the round.
Do not be tempted to hold onto a number you haven't rolled - this will
not work and won't be added to your final score.
"""
# Assume we have N dice. There are 3 rounds per turn and between
# rounds the player chooses r numbers to keep. The next round will
# then have N - r dice to roll. After each turn the kept dice
# collection is displayed.
#
# The turn stops when 3 rounds have been played or when there are no
# more dice to roll or when the player decides to stop.
def play_one_turn(dice=5, turns=3):
keep = []
msg = "Dice to hold: "
while turns and dice:
turns -= 1
print(f"Kept: {keep}")
play = random.choices(range(1, 7), k=dice)
print(f"Throw --> {play}")
for h in map(int, input(msg).split()):
if h in play:
keep.append(h)
dice -= 1
play.remove(h)
ask = "Another round? (yes/no) "
if not input(ask).strip().upper().startswith('Y'):
break
return keep
if __name__ == "__main__":
print(greeting)
score = play_one_turn()
print(f"Play result {score}")