繁体   English   中英

if 语句无缘无故抛出错误? (Python)

[英]if statement throwing up errors for no obvious reason? (Python)

我正在尝试为可以接受公制和英制测量的 BMI 计算器构建一个基本(初学者)项目,如果输入的测量单位无效(例如用户是),我试图包含raise语句以显示错误输入英制和公制或不输入。 我相信我已经正确使用了这些语句,但即使正确输入了单位,它仍然会显示错误。

import sys

unit = input("To begin, please input your units of measurement (Imperial or Metric) ")

#error if user doesn't enter imperial or metric
if unit.lower().__contains__("metric" or "imperial") == False:
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of 
    measurement is valid/spelled correctly")
    exit(1)

#error if user enters both imperial and metric
if unit.lower().__contains__("metric" and "imperial"):
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of 
    measurement is valid/singular")
    exit(1)

我已经尝试删除它们并且它有效,但我想保留它们。 我尝试将进一步的 if 语句更改为 elif,但无济于事。 我也尝试过修改语句,例如将if unit.lower().__contains__("metric" or "imperial") == False:更改为if not unit.lower().__contains__("metric" or "imperial")等并再次没有成功。 有什么可以做的吗?

编辑:这仅在输入“帝国”时发生

"metric" or "imperial"评估为"metric" ,您可以通过将其粘贴到 REPL 中看到:

>>> "metric" or "imperial"
'metric'

因此unit.lower().__contains__("metric" or "imperial")"metric" in unit.lower()相同,如果单位是“imperial”,这当然是错误的(并产生您的错误) “:

>>> unit = "imperial"
>>> "metric" in unit
False

相反,您可以在测试之间使用or

>>> "metric" in unit or "imperial" in unit
True

或将单元放入可迭代并使用any function:

>>> any(u in unit for u in ("metric", "imperial"))
True

您正在测试当前代码中是否存在 True 和 False。 一种更简单的方法是只使用in关键字,然后拆分预期的术语。

这种方式也更具可读性。

import sys

unit = input("To begin, please input your units of measurement (Imperial or Metric) ")

#error if user doesn't enter imperial or metric
if unit.lower() not  in ["metric", "imperial"]:
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of 
    measurement is valid/spelled correctly")
    sys.exit(1)

#error if user enters both imperial and metric
if "metric" in unit.lower() and "imperial" in unit.lower():
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of 
    measurement is valid/singular")
    sys.exit(1)

or不做你认为它在这里做的事情。 or将返回它找到的第一个非NoneFalse 例如: False or 12 = None or 12 = 12 or None = 12 此外,您应该尽量避免直接调用 dunder(双下划线)方法。 __contains__ in function 中。 因此,更好的实施方式是:

import sys

unit = input("To begin, please input your units of measurement (Imperial or Metric) ").lower()

#error if user doesn't enter imperial or metric
if "metric" not in unit and "imperial" not in unit:
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of measurement is valid/spelled correctly")
    exit(1)

#error if user enters both imperial and metric
if "metric" in unit and "imperial" in unit:
    sys.tracebacklimit = 0
    raise Exception("An error has occurred!\n\nPlease check if your unit of measurement is valid/singular")
    exit(1)

编辑:您的代码仅在输入“帝国”时出错的原因是由于我描述的or行为。 "metric" or "imperial"被评估为"metric"所以unit.lower().__contains__("metric" or "imperial") == Falseunit.lower().__contains__("metric") == False相同unit.lower().__contains__("metric") == False这显然不是您想要的行为。

暂无
暂无

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

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