gpt4 book ai didi

当持有两个整数骰子时,Python偶尔出现列表索引错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:24 25 4
gpt4 key购买 nike

当我选择持有两个骰子时,它并不总是遵循脚本。它发生在大约第 50 行。有时它甚至不打印列表。(作为旁注 - 如果有人可以简化代码,我们将不胜感激。)任何人都可以帮助提供解决方案和问题原因。(脚本如下)

import random
points = 0
final = []
print("Welcome to Yahtzee")
print("Where there are closed questions answer lower case with 'y' or n'")
print("Please be aware that this a variation of the traditional game.\nShould you hold a number- you cannot 'unhold' it.\nIt has been added to your final roll for the round.")
print("Do not be tempted to hold onto a number you haven't rolled\n- this will not work and won't be added to your final score")

def roll_dice():
global collection
collection = []
input("Press Enter to roll the first die")
die_1 = random.randint(1,6)
collection.append(die_1)
print(die_1)

input("Press Enter to roll the second die")
die_2 = random.randint(1,6)
collection.append(die_2)
print(die_2)

input("Press Enter to roll the third die")
die_3 = random.randint(1,6)
collection.append(die_3)
print(die_3)

input("Press Enter to roll the fourth die")
die_4 = random.randint(1,6)
collection.append(die_4)
print(die_4)

input("Press Enter to roll the fifth die")
die_5 = random.randint(1,6)
collection.append(die_5)
print(die_5)
roll_dice()
print(collection)

yeno = input("Would you like to hold any dice? ")
if yeno == "n":
input("This will mean re-rolling all the dice: proceeding...")
del(collection)
roll_dice()
print(collection)
elif yeno == "y":
no = input("How many dice would you like to hold: " )
if no == "1":
num1 = input("Enter the number you would like to keep: ")
num1 = int(num1)
for x in range(len(collection)-1):
if collection[x] == num1:
final.append(num1)
del(collection[x])
print(collection)
print(final)
if no == "2":
num2 = input("Enter the first number you would like to keep: ")
num2 = int(num2)
num3 = input("Enter the second number you would like to keep: ")
num3 = int(num3)
for x in range(len(collection)-1):
if collection[x] == num2:
final.append(num2)
del(collection[x])

for x in range(len(collection)-1):
if collection[x] == num3:
final.append(num3)
del(collection[x])
print(collection)
print(final)

最佳答案

看来您最好阅读一下 listrandom 模块所提供的内容。

下面是生成一轮 Yatzy 的建议简单解决方案。

注意:该解决方案使用格式化字符串文字,因此需要 Python 3.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}")

关于当持有两个整数骰子时,Python偶尔出现列表索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com