2016-06-12 7 views
0
answer = input('how are you') 
if answer == 'good': 
    print('glad to hear it') 
if answer == 'what?': 
    print('how are you?') 

Ohne Pause zu verwenden, wie fange ich am Anfang wieder an, wenn der Benutzer 'was?' Wie würde ich das nur mit Variablen und Schleifen machen?Wie bekomme ich Programm, um ohne Pause neu zu starten?

good = False 

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

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

Wenn die Variable goodTrue wird, stoppt die Schleife:

+1

Rekursion, eine Schleife, usw. sind alle Möglichkeiten, es zu tun. – Li357

Antwort

0

Dies sollte funktionieren. Die continue springt zur nächsten Iteration der Schleife, aber es ist nicht notwendig. Lassen Sie es dort, zeigt der Leser, dass 'what?' ein erwarteter Eingang ist.

Nun, Sie sagten, Sie nicht break verwenden können, aber, wenn Sie könnten, es würde wie folgt aussehen:

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

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

‚‘‘hält Looping bis die Antwort .good wird. Keine Flags verwendet '' '

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

Sie brauchen nichts Kompliziertes, um das zu erreichen.

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') 

oder

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') 

Der erste Fall, wenn Sie good oder die zweite erwar, wenn eine Antwort außer what? funktioniert.

Verwandte Themen