0

I'm currently writing a code that needs to be repeated but only if the user wants it to be repeated.I have put the code in a "while True" loop and tried to use if statements to repeat the code if the user enters "yes" or break the loop if the user enters "no" however the loop doesn't break no matter what the user enters. Here is what I have tried to get to work:

while True:
    question=input("do you like maths?")
    re_do=input("Would you like to check anything else?")
    if re_do.lower=="no":
        break
    elif re_do.lower=="yes":
        continue

I also need the question to be repeated if they enter something other than "yes" or "no" so I was wondering if there was a way that I could set the variable to only accept "yes" or "no" so I could use "try" and "except".

SkyBlade16395
  • 117
  • 1
  • 1
  • 12
  • 3
    Try adding parentheses to your `lower`s: `if re_do.lower()=="no":` – Kevin May 26 '15 at 14:47
  • thank you, that has fixed the first problem - I don't know how I didn't see that before, do you have any idea how I could do the second part of the question? – SkyBlade16395 May 26 '15 at 14:49
  • 1
    Also, related: [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/953482) – Kevin May 26 '15 at 14:50
  • Why don't you just add an `else`? – jonrsharpe May 26 '15 at 14:50
  • 1
    If the lowercase input is not "no" or "yes", the loop already just repeats. – chepner May 26 '15 at 14:51
  • I need the 2nd question to repeat and I only know how to do that by putting the whole thing apart from the 1st question in a while loop but then I don't know how to get both the loops to break when the user inputs "no" – SkyBlade16395 May 26 '15 at 14:53
  • Turn continue into a function call and add a break after it. Then you can leave the loop as is. – Rob May 26 '15 at 14:55
  • how do you turn the continue into a function? – SkyBlade16395 May 26 '15 at 14:56
  • If the user enters "yes" you have to ask him a new question, you can't ask the same question again, I guess. So, what happens when the uses inputs "yes"? – Joe T. Boka May 26 '15 at 15:19
  • If the user enters "yes" i need it to go back to: question=input("do you like maths?") – SkyBlade16395 May 26 '15 at 15:32
  • so if the use enters "yes", he gets the same question again and if he enters "yes" again he get the same question...over and over again? Why? – Joe T. Boka May 26 '15 at 15:33
  • that is just a piece of the code I have made, in th rest of it, it opens and reads a file of the users choice so it asks the user if they want to view anything else meaning do the a. want to view the data again or b. view some other data – SkyBlade16395 May 26 '15 at 15:37
  • You should be able to do this with `if statements` only without the `while loop` – Joe T. Boka May 26 '15 at 15:45
  • how? I need it to go through the code again from: re_do=input("Would you like to check anything else?") – SkyBlade16395 May 26 '15 at 15:49
  • Your code is doing what you want already, I don't understand what the issue is. – Joe T. Boka May 26 '15 at 15:59
  • if the user enters something other than "yes" or "no" I need the code to repeat: "Would you like to check anything else?" along with something like "an invalid input has been entered" but at the moment it just goes back to the very beginning of the code again. – SkyBlade16395 May 26 '15 at 16:02

2 Answers2

1

This answer is based on your original question which I am not sure if I fully understand. Also in your comments I was trying to understand what you are trying to achieve and I am not sure if I get it but I hope it helps:

while True:
    question=input("do you like maths?")
    re_do=input("Would you like to check anything else?")
    if re_do.lower() =="yes":
        continue
    elif re_do.lower() =="no":
        print "Goodbye!"
        break
    elif re_do.lower() !="no" and re_do.lower() !="yes":
        x = input("Yes or No answer only: ")
        if x == "yes":
            continue
        else:
            print "Goodbye"
            break
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • Thank you, this has helped a lot but I don't need the code to repeat "do you like maths" until it has asked the user if the "want to check anything else" so have adapted it slightly. For future reference, do you know if it is possible to set a variable to only accept a certain input apart from using if/elif? – SkyBlade16395 May 26 '15 at 18:37
  • I have added what I have changed as an answer, I would be grateful if you could read through it and comment if you have any ways that I could improve it – SkyBlade16395 May 26 '15 at 18:49
0

After some help from various users in the comments section and from @Joe R I have found an answer to this problem:

while True:
    question=input("do you like maths?")
    while True:
        re_do=input("Would you like to check anything else?")
        if re_do.lower=="no":
            quit()
        elif re_do.lower=="yes":
            break
        elif re_do.lower() !="no" and re_do.lower() !="yes":
            print("Yes or No answer only: ")
            continue
Community
  • 1
  • 1
SkyBlade16395
  • 117
  • 1
  • 1
  • 12