2017-05-04 5 views
0

편집 : 그냥 간단한 질문 그럼 좋겠지 만 모두가 그렇습니다.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() 

미안하지만, 나는 이전의 모든 nighter로부터 꽤 피곤하고 아마도 약간 지쳤습니다.

+0

continue 문은 루프의 나머지 부분을 건너 뛰는 데 사용됩니다. 룰러 예제를 제공 할 수 있습니까? – 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

그것은 나에게 잘 돌아 간다. 직접 Enter 키를 누르지 마십시오. 먼저 "예"라고 적고 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 루프 내부해야 다시 롤링의 입력 및 결코 두 번째 입력 쿼리에 도달하지 않습니다.