2014-11-08 54 views
1

此脚本不会传递第一个if表达式! 如果用户输入DD或F,那么脚本就像if状态为真。Python如果表达式在原始输入中无法正常工作

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) 

回答

2

if语句将始终评估为True目前。

if choice == "D" or "d"在等于“D”的choice的值或文字“d”的值为真的情况下计算为True;第二部分因此总是True

相反,使用

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

choice.lower()或choice.upper()然后测试一次。 – 2014-11-08 03:35:00

+0

@RafaelBarros我不认为这将提供很多优势,调用函数将永远更昂贵,使用文字 – 2014-11-08 03:37:47

+0

我明白你的观点,我同意。另外:更昂贵。 – 2014-11-08 03:41:14

相关问题