2016-06-12 2 views

답변

0

이 제대로 작동해야

good = False 

while not good: 
    answer = input('how are you?') 
    if answer == 'what?': 
     continue 

    if answer == 'good': 
     good = True 
     print('glad to hear it') 

를 가변 goodTrue가되면, 루프가 정지한다. continue은 루프의 다음 반복으로 건너 뜁니다. 그러나 필요하지는 않습니다. 거기에 그대로 두는 것은 독자가 'what?'이 예상되는 입력임을 보여줍니다.

while True: 
    answer = input('how are you?') 
    if answer == 'what?': 
     continue 

    if answer == 'good': 
     print('glad to hear it') 
     break 
1

는 ''는 대답이 .good 될 때까지 반복 유지 ':

지금, 당신은 당신이 그것과 같을 것이다 할 수 있다면 당신은, break를 사용하지만 수 없습니다 말했다. 플래그가 사용되지 않습니다. '' '

answer='#' 
while(answer != 'good'): 
    answer = input('how are you\n') 
    if answer == 'good': 
     print('glad to hear it') 
0

달성하기 위해 복잡한 것은 필요하지 않습니다.

input = '' #nothing in input 
while input != 'good': #true the first time 
    input = raw_input('how are you?') #assign user input to input 
if input == 'good': #if it's good print message 
    print('glad to hear it') 

또는

input = 'what?' #what? in input 
while input == 'what?': #true the first time 
    input = raw_input('how are you?') #assign user input to input 
if input == 'good': #if it's good print message 
    print('glad to hear it') 
else: 
    print('too bad') 

첫 번째 경우

어떤 응답이 what?를 제외하고 작동하는 경우 good을 기대하거나 두 번째하는 경우.

관련 문제