-4

I am running the code below, where program = MainFormula() defines a function used in my program. What I want to achieve is that the program last until the input to the question - "Do you want to change alpha? (Yes/No)" is No. I get what I want, but the program gives me two times "Do you want to change alpha? (Yes/No)" once I reply to the question as "Yes". Can anyone help me to avoid asking "Do you want to change alpha? (Yes/No)" "Do you want to change alpha? (Yes/No)" two times? (just one is needed)

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.")
wpercy
  • 9,636
  • 4
  • 33
  • 45
Alberto Alvarez
  • 805
  • 3
  • 11
  • 20
  • 2
    format your code please – Ma0 Jul 18 '17 at 11:01
  • Have you tried a [while-loop](https://www.tutorialspoint.com/python/python_while_loop.htm) or [recursion](http://www.python-course.eu/recursive_functions.php)? – P. Siehr Jul 18 '17 at 11:05
  • 1
    Regardless (or perhaps because of) the formatting, I can't see how your code asks the question twice isf you say "Yes". – doctorlove Jul 18 '17 at 11:06

2 Answers2

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

I believe this is what you're trying to accomplish - let me know if this is not exactly what you're looking for and I will adjust my answer accordingly.

flevinkelming
  • 690
  • 7
  • 8
0

With some formatiing you appear to have this

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.")

A simple thing to do this avoid asking the question twice:

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

print("Congratulations! You are done with the task.")
doctorlove
  • 18,872
  • 2
  • 46
  • 62