2017-05-04 4 views
0

編集: ちょっと簡単な質問 これは素晴らしいことですが、私の心配はYESとYESを入力するとどうなりますか?continueループの誤用、大文字と小文字の回答での比較入力

import sys 
import random 

roll_again = "yes" 


msg=input('Dice Rolling Simulator. Let the dices be ever in your favour. Press ENTER to start our game of luck') 
if msg!=input: 
    pass 

while roll_again == "yes": 
    min = 1 
    max = 6 
    face = random.randint (min,max) 
    print (face) 

cmd=input("Would you like to roll the dice again? Type yes if you do.") 

if cmd != roll_again : #if it is not 'yes' then system automatically exits. 
print ("One who doesn't throw the dice can never expect to score a six. -Navjot Singh Sidhu") 
sys.exit() 

申し訳ありません、申し訳ありません、私はかなり以前はすべてのナイターに疲れています。

+0

continueステートメントは、ループの残りの部分をスキップするために使用されます。 ullerの例を教えてください。 – gonczor

+0

複数の 'if'を持たないでください。入力された文字列を小文字に変換し、それを 'はい'と比較するだけです。完全なループを表示すると、不完全なコードからエラーを判断することは困難です。 –

答えて

0
import random 

roll_again = "yes" 


msg = input('Dice Rolling Simulator. Let the dices be ever in your favour. Press ENTER to start our game of luck\n') 

# While the input in lower case is equal to "yes" a dice will be rolled 
while msg.lower() == "yes": 
    min = 1 
    max = 6 
    face = random.randint (min,max) 
    print (face) 
    msg = input("Would you like to roll the dice again? Type yes if you do.\n") 

# we get here when the input in lower case is not equal to "yes 
print("One who doesn't throw the dice can never expect to score a six. -Navjot Singh Sidhu") 
# no need of sys.exit() because system will exit anyway 
+0

こんにちは。私はコードを実行しようとした、すぐに私は入力ヒットすぐに、引用符に行きました:0 –

+0

それは私によってうまく動作します。直接入力しないでください。最初に "yes"を書いてからenterを押します。 Enterを押すと 'msg =" "'が出ます。参照してください。 –

+0

わかりました –

0

を助けてください、私はこれがあなたはそれが何をしたいんだと思う:

import sys 
import random 

roll_again = "yes" 

input("Dice Rolling Simulator. Let the dices be ever in your favour. Press ENTER to start our game of luck") 

while True: 
    min = 1 
    max = 6 
    face = random.randint(min,max) 
    print(face) 

    cmd = input("Would you like to roll the dice again? Type yes if you do.") 
    if cmd.lower() != roll_again: #if it is not 'yes' then system automatically exits. 
     print("One who doesn't throw the dice can never expect to score a six. -Navjot Singh Sidhu") 
     sys.exit() 

あなたが今持っていることは、無限ループに入っているため、再び圧延入力は、whileループ内にある必要がありますし、 2番目の入力クエリには到達しません。

関連する問題