2017-11-05 3 views
1

2つのダイスを保持することを選択すると、必ずしもそのスクリプトに従うとは限りません。それはおよそ50行目で起こります。時々、リストを印刷することさえありません。 (誰かがコードを単純化することができればそれで大いに感謝します) 誰かが解決策と問題の原因を助けることができます。 (スクリプトは以下の通りです)2つの整数ダイスを保持したときのPythonの時折リストインデックスエラー

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) 
+0

2つのダイスを持っているとはどういう意味ですか?私はちょうどあなたのプログラムを走らせました。そして私はenterを押している間、私に5つの乱数のリストを印刷しました。 – alDiablo

+0

プログラムはyahtzeeのゲームを表します:あなたはサイコロを5回賭けます。残念ながら、私はそれを続ける前にエラーが発生しました。明らかにあなたは5つのサイコロの任意の番号を保持することができますが、私は2つのサイコロの結果をコーディングしているだけです。 – Seb

+0

現在は1度だけロールします。だから、あなたはいくつのサイコロを保持するかを決めるとどうなりますか? – FredrikHedman

答えて

1

は、あなたが何をlistrandomモジュールを提供している上に読み取ることがよいでしょうようです。

Yatzyの1つのラウンドを生成するための推奨される単純なソリューション以下。

注:このソリューションでは、フォーマットされた文字列リテラルを使用しているため、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}") 
+0

ありがとう!それは私の問題のすべてに、そして理解するのに十分な簡単なコードに答えました! – Seb

+0

次に、回答を受け入れたものとしてマークする必要があります。 – Wodin

関連する問題