2017-07-18 4 views
-4

Ich verwende den folgenden Code, wobei program = MainFormula() eine Funktion definiert, die in meinem Programm verwendet wird. Was ich erreichen will ist, dass das Programm bis zur Eingabe der Frage - "Möchtest du Alpha ändern? (Ja/Nein)" ist Nein. Ich bekomme, was ich will, aber das Programm gibt mir zweimal "Do you möchte Alpha ändern (Ja/Nein) "sobald ich die Frage mit" Ja "beantworte. Kann mir jemand helfen, nicht zu fragen: "Willst du Alpha ändern? (Ja/Nein)" "Willst du Alpha ändern? (Ja/Nein)" zweimal? (Nur eine benötigt wird)Wie führe ich eine Eingabeanweisung aus, bis du in Python das bekommen hast, was du willst?

input_value1 = input("Do you want to change alpha? (Yes/No) ").lower() 
while input_value1=="yes":  
    input_value1 = input("Do you want to change alpha? (Yes/No) ").lower() 
    if input_value1 == "yes":   
     program = MainFormula() print(program) 
     else: 
      print("Congratulations! You are done with the task.") 
+2

Format Code bitte –

+0

Haben Sie versucht, einen [while-Schleife ] (https://www.tutorialspoint.com/python/python_while_loop.htm) oder [Rekursion] (http://www.python-course.eu/recursive_functions.php)? –

+1

Unabhängig davon (oder vielleicht wegen der Formatierung) kann ich nicht sehen, wie der Code die Frage zweimal stellt, wenn Sie "Ja" sagen. – doctorlove

Antwort

3
while True: 
    input_value = input("Do you want to change alpha? (Yes/No) ").lower() 

    if 'no' in input_value: # When this condition is met we break from the loop 
     break 
    elif 'yes' in input_value: 
     program = MainFormula() 
     print(program) 
    else: 
     print("Congratulations! You are done with the task.") 
     break 

Ich glaube, das ist das, was Sie erreichen wollen - lassen Sie mich wissen, ob dies nicht genau das, was Sie suchen und ich werde meine Antwort entsprechend anpassen.

+0

Danke! Das wollte ich! –

+0

toll - froh, ich könnte helfen! – flevinkelming

0

Mit einigen formatiing Sie dieses

input_value1 = input("Do you want to change alpha? (Yes/No) ").lower() 
while input_value1=="yes": 
    input_value1 = input("Do you want to change alpha? (Yes/No) ").lower() 
    if input_value1 == "yes": 
     program = MainFormula() 
     print(program) 
    else: 
     print("Congratulations! You are done with the task.") 

eine einfache Sache zu haben scheinen dies zu vermeiden tun zweimal die Frage:

while input("Do you want to change alpha? (Yes/No) ").lower()=="yes": 
    program = MainFormula() 
    print(program) 

print("Congratulations! You are done with the task.") 
Verwandte Themen