2017-05-04 15 views
0

編輯: 所以這只是一個快速的問題 所以這是偉大的,所有,但我的關注是如果他們鍵入YES和是?濫用繼續循環,比較輸入的大寫和較低的答案

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語句用於跳過循環的其餘部分。你能提供更多的例子嗎? – gonczor

+0

沒有多個'if's。只需將輸入的字符串轉換爲小寫,並將其與「是」進行比較即可。你應該顯示完整的循環,很難從不完整的代碼中判斷你的錯誤。 –

回答

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

它對我來說工作得很好。不要直接回車,先寫「是」,然後按回車鍵。按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循環裏面,因爲你現在有什麼進入一個無限循環的輸入和永遠不會到達第二個輸入查詢。