answer = input('how are you')
if answer == 'good':
print('glad to hear it')
if answer == 'what?':
print('how are you?')
휴식을 사용하지 않고 사용자가 '무엇을 입력 했습니까?' 변수와 루프만으로 어떻게 할 수 있습니까?중단없이 프로그램을 다시 시작하는 방법은 무엇입니까?
answer = input('how are you')
if answer == 'good':
print('glad to hear it')
if answer == 'what?':
print('how are you?')
휴식을 사용하지 않고 사용자가 '무엇을 입력 했습니까?' 변수와 루프만으로 어떻게 할 수 있습니까?중단없이 프로그램을 다시 시작하는 방법은 무엇입니까?
이 제대로 작동해야
good = False
while not good:
answer = input('how are you?')
if answer == 'what?':
continue
if answer == 'good':
good = True
print('glad to hear it')
를 가변 good
가 True
가되면, 루프가 정지한다. continue
은 루프의 다음 반복으로 건너 뜁니다. 그러나 필요하지는 않습니다. 거기에 그대로 두는 것은 독자가 'what?'
이 예상되는 입력임을 보여줍니다.
while True:
answer = input('how are you?')
if answer == 'what?':
continue
if answer == 'good':
print('glad to hear it')
break
는 ''는 대답이 .good 될 때까지 반복 유지 ':
지금, 당신은 당신이 그것과 같을 것이다 할 수 있다면 당신은, break
를 사용하지만 수 없습니다 말했다. 플래그가 사용되지 않습니다. '' '
answer='#'
while(answer != 'good'):
answer = input('how are you\n')
if answer == 'good':
print('glad to hear it')
달성하기 위해 복잡한 것은 필요하지 않습니다.
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
을 기대하거나 두 번째하는 경우.
재귀, 루프 등 모든 방법이 있습니다. – Li357