-2

I am trying to write a some questions with yes or no answers and want to be able to tell the user to enter yes or no if they type another string (not yes or no).

I have used a While True loop, but everytime I run this it keeps going back to q1.

while True:
q1 = input("Switch on, yes or no")
q1= q1.title()

if q1 == "No":
    print("Charge your battery")
    break

elif q1 == "Yes":
    q2 = input("Screen working?")
    q2 = q2.title()
    if q2 == "No":
        print("replace screen")
        break

    elif q2 == "Yes":
        q3 = input("Ring people?")
        q3 = q3.title()
        if q3 == "No":
            print("Check your connecting to your network")
            break

        elif q3 == "Yes":
            print("Not sure")
            break

print("Thanks for using")    
JJJ
  • 32,902
  • 20
  • 89
  • 102
mrblue
  • 1
  • 2
    Indentation matters _very_ much. – ForceBru Mar 19 '17 at 14:31
  • Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – tripleee Mar 19 '17 at 14:40

1 Answers1

1

In order to make you code work you should fix two things:

  • indentation
  • replace break with continue (take a look here about the different between break continue and pass)

The following version should work:

while True:
    q1 = input("Switch on, yes or no")
    q1= q1.title()

    if q1 == "No":
        print("Charge your battery")
        continue

    elif q1 == "Yes":
        q2 = input("Screen working?")
        q2 = q2.title()
        if q2 == "No":
            print("replace screen")
            continue

        elif q2 == "Yes":
            q3 = input("Ring people?")
            q3 = q3.title()
            if q3 == "No":
                print("Check your connecting to your network")
                continue

            elif q3 == "Yes":
                print("Not sure")
                continue

print("Thanks for using")    
Yaron
  • 10,166
  • 9
  • 45
  • 65