Python表达式无法与原始输入一起正常工作

时间:2014-11-08 03:30:07

标签: python if-statement raw-input

此脚本未通过第一个if表达式! 如果用户输入DD或F,则脚本的行为类似于if状态为真。

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)

1 个答案:

答案 0 :(得分:2)

您的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"):
    ...