简体   繁体   English

While循环不断重复python

[英]While loop keeps repeating python

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. 如果用户写“ apples”或“ oranges”失败,将提示他们再次写。

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)

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' . 你的问题重复,只要它是真实的,这个问题answer等于'apples' 它是真实的answer不是 'oranges' If you answer apples , it is true that answer is not equal to 'oranges' then, so the loop repeats. 如果您回答apples ,则answer不等于'oranges'是正确的,因此循环会重复。 One obvious solution to change or to and . 一种明显的解决方案,更改or更改为and

However a more pythonic solution is to use the not in operator with a set literal ; 但是,更Python化的解决方案是将not in运算符与设置的文字结合使用 (also you do not need to repeat the input here). (同样,您无需在此处重复input )。 Thus: 从而:

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

(PS 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.) (PS我重命名了您的变量,因为您作为input提供给input的文本是问题 ,而input返回该问题的答案 。)

交换or用于and使之成为

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

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. question总是与'apples''oranges'不同'oranges' ,因为它不能同时等于两个。

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)

You can use the in operator to avoid repeating quesiton != ... for every fruit: 您可以使用in运算符来避免对每个水果重复quesiton != ...

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

Eg 例如

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()

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 您可以使用in运算符简化逻辑,如果该运算符左侧的项等于右侧序列的元素之一,则该运算符返回True

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

ps 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. ps当两个术语均为字符串时, in的行为不同,如果左侧术语为右侧术语的子字符串,则返回True

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM