1
Another_Mark = raw_input("would you like to enter another mark? (y/n)")
while Another_Mark.lower() != "n" or Another_Mark.lower() != "y":
 Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
if Another_Mark == "y":
 print "blah"
if Another_Mark == "n":
 print "Blue"

This is not the actual code I'm using except for the 1st three lines. anyways my question is why does the while loop keep repeating even when I input a value 'y' or 'n', when it asks again if you want to input another mark on the third line. I'm stuck in a infinitely repeating loop. It shouldn't repeat when the value for Another_Mark is changed to either "y" or "n"

asked Apr 14, 2013 at 18:07
1
  • 2
    not (a or b) (what you describe) is not the same as not a or not b (what your code says). Commented Apr 14, 2013 at 18:11

4 Answers 4

5

Try:

while Another_Mark.lower() not in 'yn':
 Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")

not in operator returns true if given object is not found in the given iterable and false otherwise. So this is the solution you're looking for :)


This wasn't working due to boolean algebra error fundamentaly. As Lattyware wrote:

not (a or b) (what you describe) is not the same as not a or not b (what your code says)

>>> for a, b in itertools.product([True, False], repeat=2):
... print(a, b, not (a or b), not a or not b, sep="\t")
... 
True True False False
True False False True
False True False True
False False True True
answered Apr 14, 2013 at 18:11
Sign up to request clarification or add additional context in comments.

3 Comments

While a good solution, a little more explanation would make this answer a lot better.
I edited in a simple proof (by brute force) showing they are not the same.
The proof is not clear right away anyway, so it'll be better if op does the boolean calculations himself to verify. But thanks for contribution anyway :)
3

Your loop logic only every comes out true - if the input is "n", then it's not "y" so it's true. Conversely if it's "y" it's not "n".

Try this:

while not (Another_Mark.lower() == "n" or Another_Mark.lower() == "y"):
 Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
answered Apr 14, 2013 at 18:12

1 Comment

Alternatively, while Another_Mark.lower() != "n" and Another_Mark.lower() != "y": is also equivalent.
1

Your logic behind the looping is wrong. This should work:

while Another_Mark.lower() != "n" and Another_Mark.lower() != "y":
 Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
answered Apr 14, 2013 at 18:14

Comments

1

You need to use AND instead of OR.

It's the way boolean logic distributes. You can say:

NOT ("yes" OR "no")

Or you can distribute the NOT into the parenthesis (which is what you're trying to do) by flipping the OR to an AND:

(NOT "yes") AND (NOT "no")
answered Apr 14, 2013 at 18:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.