-3

Can anyone help me figure out what is wrong with this?

while accept1 == ("yes", "Yes", "no", "No"):
    print ("I see.")
    break
else:
    print ("That's not a valid answer!")
    accept1 = input("Do you accept?")

print ("Let's begin!")

It loops back to "Do you accept" once, then even if you put in an invalid answer, it breaks the loop anyways.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Necrex
  • 1
  • 2
    I did try and explain to you that `==` doesn't work here on your [question from yesterday](http://stackoverflow.com/questions/32726599/why-do-i-get-a-syntax-error-when-trying-to-test-for-correct-user-input). – Martijn Pieters Sep 23 '15 at 16:17
  • Moreover, you are using `while` wrong still, you *really* want to use `if` here. – Martijn Pieters Sep 23 '15 at 16:17
  • 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) – Morgan Thrapp Sep 23 '15 at 16:18
  • You seem to be having issues with the fundamentals of programming so you might want to find a book or something. [Tutorials Point](http://www.tutorialspoint.com/python/python_overview.htm) seems like a good place to start. – SirParselot Sep 23 '15 at 16:21
  • Why did you not use any of the suggestions from Martijns answer to your question yesterday? – Padraic Cunningham Sep 23 '15 at 16:23
  • @PadraicCunningham Probably because OP doesn't understand the answer. The fact that OP is using a while loop instead of an if statement seems like a good clue towards that. – SirParselot Sep 23 '15 at 16:24
  • @SirParselot, asking a similar question again and getting the same answers that have already been provided seems pretty redundant, if the OP does not understand the basics they should read a basic tutorial – Padraic Cunningham Sep 23 '15 at 16:32
  • @PadraicCunningham that's why I posted a link to a basic tutorial earlier in the comments. – SirParselot Sep 23 '15 at 16:34

2 Answers2

2

== checks for equality , so you are trying to check if the string accept1 is equal to the tuple ("yes", "Yes", "no", "No") , this would never be true. Hence you get the output - "That's not a valid answer!" .

You should use in operator to check if accept1 is equal to one of the elements of the tuple.

Other suggestions, you can use .lower() to make the complete string lowercase to check, and use set. Example -

while accept1.lower() not in {"yes", "no"}:

And your loop is wrong (You are trying to loop as long as the answer is valid, when you should be trying to loop till the answer becomes valid), it should be something like -

accept1 = input("Do you accept?")
while accept1.lower() not in {"yes", "no"}:
    print ("That's not a valid answer!")
    accept1 = input("Do you accept?")
print ("I see.")
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

I think what you want to do is this:

answers = set(['Yes', 'No', 'yes', 'no'])

if accept1 in answers :
    print ("I see.")
else:
    print ("That's not a valid answer!")
    accept1 = input("Do you accept?")

print ("Let's begin!")

You want the inputed value to be "yes", "Yes", "no" or "No", right?

jlnabais
  • 829
  • 1
  • 6
  • 18
  • 1
    Also, you can use a *set* in Python 3, for efficiency. Python optimises for that case. – Martijn Pieters Sep 23 '15 at 16:18
  • 1
    There's no module named `sets`. You can just write `answers = {'Yes', 'No', 'yes', 'no'}` – saulspatz Sep 23 '15 at 16:34
  • Thanks I have edited. I don't usually use sets, so I searched and I've found some documentation on a deprecated lib ([here](https://docs.python.org/2/library/sets.html)). Thanks for the tip. – jlnabais Sep 23 '15 at 16:46