2016-09-17 7 views
0

私はクラスの単純なyahtzeeスタイルのダイスゲームに取り組んでいますが、私は問題にぶち当たっています。つまり今はリストに問題があります。私はプログラムを実行するときに関数とリストのインデックスが範囲外にある

File "C:/Users/u1069284/Desktop/HW3_LandonShoaf.py", line 90, in scoring 
    counts[value] = counts[value] + 1 

IndexError: list index out of range 

を実行しています。私は助けてもらえないのですか?

from math import * 
    from random import * 
    global PlayerScore 
    PlayerScore = 100 

def firstroll(): 

    global dice 
    dice = [0,0,0,0,0] 
    print(dice) 

    for Index in range(5): 
      dice[Index] = randint(1,6) 
    print(dice) 
    return(dice) 
def reroll(): 
    rerollstring = input("Which die/dice do you want to reroll?: ") 
    print(rerollstring) 

    rerolllist = rerollstring.split() 

    print(rerolllist) 

    for i in rerolllist: 
     dice[int(i) - 1] = randint(1,6) 
    print(dice) 
    return(dice) 
def scoring(): 

    global dice 
    counts = [] * 7 
    for value in dice: 
     counts[value] = counts[value] + 1 

    if 5 in counts: 
     message = "Five Of A Kind! +30 Points!" 
     score = 30 
    elif 4 in counts: 
     message = "Four Of A Kind! +25 Points!" 
     score = 25 
    elif (3 in counts) and (2 in counts): 
     message = "FULL HOUSE! +15 Points!" 
     score = 15 
    elif 3 in counts: 
     message = "Three Of A Kind! +10 Points!" 
     score = 10 
    elif counts.count(2) == 2: 
     message = "Two Pairs! +5 Points" 
     score = 5 
    elif not (2 in counts) and (counts[1] == 0 or counts[6] == 0): 
     message = "Straight! +20 Points!" 
     score = 20 
    else: 
     message = "NO POINTS!" 
     score = 0 
    return(score) 
    print(message) 
def PlayerScoring(): 
    PlayerScore == PlayerScore - 10 
    PlayerScore == PlayerScore + score 
    return(PlayerScore) 
    print(PlayerScore) 
def want2play(): 
    play = input("Play the dice game? (y for yes): ") 

    return play 
def main(): 
    while True: 
     if int(PlayerScore) > 0: 
      playround = want2play() 
      if playround == 'y': 
       firstroll() 
       reroll() 
       scoring() 
       PlayerScoring() 
      else: 
       print("Game Over") 
       break 
     else: 
      print("Your Are Out Of Points!") 
      print("GAME OVER") 
main() 

答えて

0

これを試してみてください:

counts = [0]*7 
for value in dice: 
    counts[value] += 1 
0

この:

counts = [] * 7 
for value in dice: 
    counts[value] = counts[value] + 1 

あなたが期待しているとして、0値のリストを初期化するための有効な方法ではありません。通訳で[] * 7を印刷するだけで、あなたの前提が正しくないことがわかります。空のリストが生成されます。私はあなたのコードが0のリストを生成すると信じられていたのかどうかはわかりません。

代わりに、counts = [] * 7counts = [0 for _ in range(6)]に置き換えて、インデックスがゼロであることに注意してください。これにより、リスト内の冗長な最初の要素を避けることができます。

関連する問題