0

How would I add a while loop to my program where if the user doesn't enter "yes" or "no" to one of the questions they are asked to try again? Would I have to add it to each question or at the end of the program?

name = input("What is your name? ")
while True:
    try:
        income = int(input("What is your income? ($) "))
        break
    except ValueError:
        print ("Invalid input\nPlease enter a figure")
        continue
    else:
        break




print("Please answer the questions with 'yes' and 'no'")

Q1 = "Q1: Do you receive an income tested benefit? "
Q2 = "Q2: Is this tax code for the income tested benefit? "
Q3 = "Q3: Is this tax code for your main or highest source of income? "
Q4 = "Q4: Are you a New Zealand tax resident? "
Q5 = "Q5: Is your annual income likely to be between $24,000 and $48,000? "
Q6 = "Q6: Are you or your partner entiled to Working for Families Tax Credits or\n\
an overseas equivalent or do you receieve NZ Super, veteran's pension or an\n\
overseas equivalent? "
Q7 = "Q7: Do you have a NZ student loan? "

M1  = "This program cannot determine your tax code. Please use the program for secondary income "

A1 = input(Q1)
if A1 == "yes":
              A2=input(Q2)
              if A2=="yes":
                           TaxCode = "M"                            
              elif A2=="no":
                           print(M1)
elif A1=="no":
              A3=input(Q3)
              if A3=="no":
                            print(M1)
              elif A3=="yes":
                            A4=input(Q4) 
                            if A4=="yes":
                                          A5=input(Q5)
                                          if A5=="yes":
                                                        A6=input(Q6)
                                                        if A6=="no":
                                                                     A7 = input(Q7)
                                                                     if A7=="yes":
                                                                                  TaxCode="ME SL"
                                                                     elif A7=="no":
                                                                                  TaxCode="ME"

                                                        elif A6=="yes":
                                                                      A7=input(Q7)
                                                                      if A7=="yes":
                                                                                    TaxCode="M SL"
                                                                      if A7=="no":
                                                                                    TaxCode="M"
                                          elif A5=="no":
                                                        A7=input(Q7)
                                                        if A7=="yes":
                                                                      TaxCode="M SL"
                                                        elif A7=="no":
                                                                      TaxCode="M"
                            if A4=="no":
                                          A7=input(Q7)
                                          if A7=="yes":
                                                        TaxCode="M SL"
                                          elif A7=="no":
                                                        TaxCode="M"


print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)
  • I'm still having trouble. I added while True #as the first line of code continue else: break #as the last three and get the error 'break' outside of loop –  Apr 19 '17 at 07:55
  • Please help, I'm struggling to implement the loop into my program.. :( –  Apr 19 '17 at 21:18

0 Answers0