2016-11-25 12 views
1

私はシンプルなダイスローリングプログラムを作ろうとしましたが、私はサイコロを「ロール」にすることはできますが、止めることはできません。私はそれを追加しようとしたので、もしあなたが 'いいえ'または 'いいえ'と言うなら、それはに戻ります。そしていつものように私の他の機能に行きます。私は他のコードへの継続が重要なので、私はリターンを言う。ありがとうございました。roll_more == 'no'の場合ループが終了しない

import random 
min = 1 
def rolling6(): 
    roll_more = "yes" 
    while roll_more == "yes" or roll_more =="y": 
     max = 6 
     print("Rolling the dices...") 
     print("The values are...") 
     print(random.randint(min, max)) 
     print(random.randint(min, max)) 
     roll = input("Roll the die again? ") 

    if roll_more == "no" or roll_more == "n": 
     return 
    #roll_again = "yes" 
    #while roll_again == "yes" or roll_again =="y": 
    # print("Rolling the dices...") 
    # print("The values are...") 
    # print(random.randint(min, 6)) 
    # print(random.randint(min, 6)) 
    # roll_again = input("Roll the die again? ") 

    #if roll_again == "no" or roll_again == "n": 
    # return 


x = input("Use six sided die? ") 
while x == "yes" or x =="y": 
    rolling6() 
+1

ユーザー入力は変数 'roll'に割り当てられ、' roll_more'をチェックしています。スペースを節約し、小文字が使用されていることを確認するには、['yes'、 'y']: 'のroll_more.lower()のようにチェックを行うこともできます。 'No'の値はコードによって' yes'と解釈されます。 –

答えて

1

代わりroll_morerollに書いているので、あなたは常に一定で、「はい」に対してチェックされています。

さらに、xroll_moreの変数には、期待する値が格納されていません。まず、 "yes"を入力してループに入り、rolling6が呼び出されます。次に、rolling6ループを入力します。 "no"を入力して終了すると、外側のループに出て、xの値が "yes"(それはあなたが決して上書きしていないので)という値を持ちます。そのループに戻り、もう一度rolling6に再度入力してください。

rollroll_moreに変更し、while x == "yes" or x =="y":if x == "yes" or x =="y":に変更します。

また、if roll_more == "no" or roll_more == "n": returnは、そのステートメントの後にコードがないため、冗長です。

関連する問題