Python if表达式与原始输入不起作用

2024-09-28 05:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

此脚本没有通过第一个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)

Tags: ortheforinputyoursizebandraw
1条回答
网友
1楼 · 发布于 2024-09-28 05:24:05

您的if语句当前的计算结果总是True。你知道吗

if choice == "D" or "d"choice的值等于“D”或文字“D”的值为真时求值为真;因此第二部分总是True。你知道吗

相反,使用

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

相关问题 更多 >

    热门问题