-3
result = {}
question = '你叫什么名字? '  #What is your name
question_2 = '如果你能去世界上的任何一个地方度假,你想去哪? '  #If you could go to any place in the world for vacation, where would you like to go?
question_3 = '你愿意让你旁边的人也来参加这个调查吗? (yes/ no) '  #Would you like to let people next to you participate in this survey?
while True:
    name = input(question)
    place = input(question_2)
    result[name] = place                                  #除了yes或者no不允许输入其他字符
    while True:                                           #No other characters are allowed except "yes" or "no"
        opinion = input(question_3)
        if opinion.lower() != 'yes' or 'no':
            print('请重新输入')   #please enter again
        else:
            break
    if opinion == 'no':
        break

No matter what you enter after running, you can't jump out of the loop

if opinion.lower() not in ('yes','no'):

It's normal to change to this, but I'm still curious why something went wrong

Beginners, thanks

  • 5
    Python is not English, despite sometimes looking quite a lot like natural language. In Python, `"yes" or "no"` is an expression that evaluates to `"yes"` – juanpa.arrivillaga Dec 26 '20 at 14:57
  • 1
    @juanpa.arrivillaga: Although due to precedence, `opinion.lower() != 'yes' or 'no'` is treated as `(opinion.lower() != 'yes') or 'no'` (which still doesn't mean what the questioner wanted). – user2357112 Dec 26 '20 at 15:05

2 Answers2

1

Consider this line:

if opinion.lower() != 'yes' or 'no':

So this is how that expression is evaluated (according to order of precedence):

if (opinion.lower() != 'yes') or ('no'):

And 'no' is always evaluated as True. Moreover, it should be option not 'yes' 'and' not 'no' (instead of 'or'). Consider changing it to:

if opinion.lower() != 'yes' and opinion.lower() != 'no':

More shortly,

if opinion.lower() not in ('yes', 'no'):

And this will fix your issue.

0
result = {}
question = "What is your name"
question_2 = "If you could go to any place in the world for vacation, where would you like to go?"
question_3 = "Would you like to let people next to you participate in this survey?"
while True:
    name = input(question)
    place = input(question_2)
    result[name] = place                                  #除了yes或者no不允许输入其他字符
    while True:                                           #No other characters are allowed except "yes" or "no"
        opinion = input(question_3)
        if opinion.lower() not in ('yes','no'):
            print('please enter again')
        else:
            break
    if opinion == 'no':
        break

You could use a tuple to fix your problem, that's one thing first.

Now what you want is why isn't the following code working:

x != "yes" or "no"

Recall from the order of precedence that != has a higher priority than or, so x!= "yes" will be evaluated first, then it will be ORed with "no", simply to fix it, add parenthesis around the or statement:

x != ("yes" or "no")

Would do the trick for ya!

aliberro
  • 530
  • 2
  • 9
  • 1
    ('yes' or 'no') evaluates to 'yes' so 'yes' != ('yes' or 'no') gives False but 'no'!=('yes' or 'no') gives True – simon Dec 26 '20 at 15:36
  • 1) the tuple is already mentioned in the question and 2) adding parentheses doesn't solve anything – Guy Incognito Dec 26 '20 at 15:56