如何在python中生成OR语句以供用户选择?

2024-10-02 18:14:05 发布

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

为什么它总是只打印升序

if choose=="A" or "a":
    print("Ascending order: " ,Anewlist)
elif choose=="D" or "d":
    print("Descending order: " ,Dnewlist)
elif choose=="B" or "b":
    print("Ascending order: " ,Anewlist)
    print("Descending order: " ,Dnewlist)
else:
    print("try again")


Tags: oriforderelseprinttryelifagain
2条回答

您还需要在or命令的另一端指定相等性测试:

if choose == "A" or choose == "a":
    print("Ascending order: ", Anewlist)

或者,将choose变量设为大写:

if choose.upper() == "A":
    print("Ascending order: ", Anewlist)

您应该使用:

if choose == "A" or choose == "a":

或:

if choose in ("A", "a"):

相关问题 更多 >