Python if 表达式无法正确处理原始输入

标签 python if-statement raw-input

该脚本没有通过第一个 if 表达式! 如果用户输入 DD 或 F,则脚本的行为就像 if 状态为 true。

choice = raw_input("Cup size for bra: D, DD, or F: ")
if choice == "D" or "d":
    band_length = raw_input("Please enter the bra band length for your D size breasts: ")
    D_Statistics(band_length)
elif choice == "DD" or "dd":
    band_length = raw_input("Please enter the bra band length for your DD size breasts: ")
    DD_statistics(band_length)
elif choice == "F" or "f":
    band_length = raw_input("Please enter the bra band length for your F size breasts: ")
    F_statistics(band_length)

最佳答案

当前,您的 if 语句的计算结果始终为 True

if choice == "D"or "d"choice 的值等于“D”或文字“d”的值时计算结果为 True是真实的;因此第二部分始终为 True

相反,使用

if choice in ("D", "d"):
    ...
elif choice in ("DD", "dd"):
    ...
if choice in ("F", "f"):
    ...

关于Python if 表达式无法正确处理原始输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813411/

相关文章:

python - Django 版本 : incompatible version data during field_dict access

python - 使用 Pandas 将数据框中的变量拆分为行

performance - Case 与 If Else If : Which is more efficient?

class - yii2详细 View 条件行类

Python 原始输入

raw-input - 是否可以实现全局原始输入?

python - 如何将纯 http 请求发送到 url?

python - pandas df 对多列中的部分字符串值进行过滤

java - 对我的 if 循环没有运行感到有点困惑?

python - 如何在 while 循环中使用 raw_input()