Python 2.7忽略else并合并了“ if not”语句

时间:2018-12-11 03:55:17

标签: python if-statement

我不是Python的新手,但直到最近才开始更多地使用它。除了模棱两可的问题之外,在弄清楚为什么我的代码没有监听提供的elseif not语句的原因方面,我需要帮助。

My code is way too big to place into a code block, but necessary for context. Here's the link to a Google Docs document, with the problematic piece highlighted.

应该发生的情况是,当提示您输入性别时,放置除“ m”,“ f”或“ o”(或大写字母)之外的其他任何内容都会抛出{{1 }}语句。但是,这不会发生,并且默认为您输入的是“ m”。

2 个答案:

答案 0 :(得分:1)

if sex == 'm' or sex == 'M':

... or 'M'始终为真

答案 1 :(得分:0)

sex == "m" or "M"不是sex == 'm' or sex == 'M',而是(sex == 'm') or 'M'。由于True不是空字符串,因此它始终为"M"

对于您的情况,我建议您使用if sex in ('m', 'M')sex in ("f", "F")等。

相关问题