2017-03-24 4 views
-2

私は2つのダイスを転がすためのこの小さなプログラムにいくつか問題があります。なぜこのループは終了する前に停止しますか?

なぜプログラムがループを終了する前に停止し、代わりにループに「もう一度再生しますか?

ありがとうございました!

#Program which simulates the rolling of two dice 

import random 

def rolling_dices(repetitions): 
    a = repetitions 
    b = 1 
    while b <= a: 
     i = (random.randrange(1,7)) 
     y = (random.randrange(1,7)) 
     b +=1 
     print(i, y, "\t =>", int(i+y)) 

     answer = input("do you want to play again? (Y/N)") 
     if answer.lower() == "y": 
      continue 
     else: 
      break 


rolling_dices(5) 
+0

問題に関連していない可能性があります。インデントで – davedwards

+0

こんにちは!私はそれを修正しましたか? –

+0

はい、インデントを修正しましたが、問題が修正されたかどうかはわかりません。ループが終了する前に「停止する」という意味ですか?それは5回ループする。それは何をすべきか? – davedwards

答えて

2

はようで、代わりに質問のプロンプトを使ってサイコロのループをループに入れてください。

import random 

def rolling_dices(repetitions): 
    a = repetitions 
    b = 1 

    while b <= a: 
     i = (random.randrange(1,7)) 
     y = (random.randrange(1,7)) 
     b +=1 
     print(i, y, "\t =>", int(i+y)) 

rolling_dices(5) 

while input("do you want to play again? (Y/N)").lower() == "y": 
    rolling_dices(5) 

print("done.") 
+0

私は答えのためにループ中に別のループを置くとは思わなかった素晴らしい解決策! –

0

正しくwhileループインデントすることを確認します:Pythonでインデントの

#Program which simulates the rolling of two dice 

import random 

def rolling_dices(repetitions): 
    a = repetitions 
    b = 1 

    while b <= a: 
     i = (random.randrange(1,7)) 
     y = (random.randrange(1,7)) 
     b +=1 
     print(i, y, "\t =>", int(i+y)) 

     answer = input("do you want to play again? (Y/N)") 
     if answer.lower() == "y": 
      continue 
     else: 
      break 


rolling_dices(5) 

さらに詳しい情報:あなたはサイコロ圧延ループから質問を削除するようhttp://www.diveintopython.net/getting_to_know_python/indenting_code.html

関連する問題