0

I've been making a very simple program which asks the user if they prefer apples or oranges. If the user writes apples, you will receive the message 'You prefer apples', and vice versa with oranges. If the user fails to write either 'apples' or 'oranges' they will be prompted to do it again.

For some reason however, regardless of if the user wrote 'apples' or 'oranges' it will still prompt them to write in their answer again. Here is an example.

Here is my code:

question = input('Do you prefer apples or oranges? ').lower()

while question!='apples' or question!='oranges':
    question = input('Do you prefer apples or oranges? ').lower()

print('You prefer ' + question)
0

6 Answers 6

5

Your question repeats the question for as long as it is true that answer is not equal to 'apples' or it is true that the answer is not 'oranges'. If you answer apples, it is true that answer is not equal to 'oranges' then, so the loop repeats. One obvious solution to change or to and.

However a more pythonic solution is to use the not in operator with a set literal; (also you do not need to repeat the input here). Thus:

answer = None
while answer not in {'apples', 'oranges'}:
    answer = input('Do you prefer apples or oranges? ').lower()

(P.S. I renamed your variable, since the text that you give to input as an argument is the question, and input returns the answer to that question.)

Sign up to request clarification or add additional context in comments.

Comments

0

It is always the case that question is different from 'apples' or different from 'oranges', because it cannot be equal to both at the same time.

What you want to express is this:

question = input('Do you prefer apples or oranges? ').lower()

while question not in ('apples', 'oranges'):
    question = input('Do you prefer apples or oranges? ').lower()

print('You prefer ' + question)

Comments

0

You can simplify your logic using the in operator that returns True if the term on its left is equal to one of the elements of the sequence on its right

answer = ''
while answer not in ('oranges', 'apples'):
    answer = input('...')

p.s. the behavior of in when both terms are strings is different, it returns True if the left term is a substring of the right term.

Comments

0

Try this:

while not ((question=='apples') or (question=='oranges')):

Comments

0

You can use the in operator to avoid repeating quesiton != ... for every fruit:

while question not in LIST/SET/TUPLE/DICTIONARY/STRING:

E.g.

while question not in ["apples", "oranges"]:
    question = input('Do you prefer apples or oranges? ').lower()

Or declare a "fruit list" beforehand:

fruits = ["apples", "oranges"]

while question not in set(fruits):
    question = input('Do you prefer apples or oranges? ').lower()

2 Comments

Python 3, so use a set literal {'apples', 'oranges'}.
@AnttiHaapala Yeah, good point. I just didn't want to assume that the OP was working with unique values. set(fruits) would make sense in the comparison/look-up though.
0

exchange or for and so that it becomes

question!='apples' and question!='oranges'

Comments

Your Answer

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.