python - 在 python 中使用 while 检查错误

标签 python python-3.x

while True:
    self.soil_type = input("Please choose soil type - alkaline, neutral, acidic: ")
    print('-'*30)
    print('')
    if self.soil_type != "alkaline" and "neutral" and "acidic":
        print("***NOT A VALID SOIL TYPE***")
        continue
    else:
        False

我想用 while 循环进行错误检查。 如果我输入的不是碱性、中性、酸性,则打印“不是有效的土壤类型”并继续输入命令。如果我输入正确的土壤类型之一,则退出循环并转到下一步。

但是没有成功。即使我输入正确的一个(例如“酸性”),它也会显示“不是有效的土壤类型”。我缺少什么? :)

最佳答案

while True:
    self.soil_type = input("Please choose soil type - alkaline, neutral, acidic: ")
    print('-'*30)
    print('')
    if not(self.soil_type == "alkaline" or self.soil_type == "neutral" or self.soil_type == "acidic"):
        print("***NOT A VALID SOIL TYPE***")
        continue
    else:
        break

尝试使用这个......它应该可以工作!

关于python - 在 python 中使用 while 检查错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095945/

相关文章:

Python3 :requests module headers

python - 从 python 并行写入 MongoDb 集合

python - 如何创建一个在后台运行并对键盘输入使用react的 Python 脚本?

python - 在 Python 中将不需要的日期时间格式的数据值更改为带破折号的数字

Python:如何使这个颜色阈值功能更有效

BeautifulSoup 的 Python 内存问题

Python urllib3 错误 - 导入错误 : cannot import name UnrewindableBodyError

python-3.x - 使用 Pandas 和 Python3 获取特定时间列的值

python - 基于共享 key 组合数据帧

python - 计算快速傅里叶变换中的频率