Python 当if语句选中';咖啡';或';c';我想要的是也能够检查所有形式的那些。也就是说,咖啡应该有用,但不是咖啡

Python 当if语句选中';咖啡';或';c';我想要的是也能够检查所有形式的那些。也就是说,咖啡应该有用,但不是咖啡,python,Python,如果用户键入coFfeE打印1,我希望它。或者如果用户键入C到键入2。基本上,我不希望大小写成为一个问题 比较if子句的大小写 coffeortea = input("Would you like coffee or tea? ") if 'coffee' == coffeortea: print("1") elif 'c' == coffeortea: print("2") else: print("3") 比较if子句的大小写 coffeortea = input(

如果用户键入coFfeE打印1,我希望它。或者如果用户键入C到键入2。基本上,我不希望大小写成为一个问题

比较if子句的大小写

coffeortea = input("Would you like coffee or tea? ")
if 'coffee' == coffeortea:
    print("1")
elif 'c' == coffeortea:
    print("2")
else:
    print("3")

比较if子句的大小写

coffeortea = input("Would you like coffee or tea? ")
if 'coffee' == coffeortea:
    print("1")
elif 'c' == coffeortea:
    print("2")
else:
    print("3")

您可以通过调用
lower()
函数,将输入正确转换为
str
,然后根据以下条件设置条件:

coffeortea = input("Would you like coffee or tea? ")
if 'coffee' == coffeortea.lower():
    print("1")
elif 'c' == coffeortea.lower():
    print("2")
else:
    print("3")

您可以通过调用
lower()
函数,将输入正确转换为
str
,然后根据以下条件设置条件:

coffeortea = input("Would you like coffee or tea? ")
if 'coffee' == coffeortea.lower():
    print("1")
elif 'c' == coffeortea.lower():
    print("2")
else:
    print("3")

可能重复的强制检查执行
str.lower()
可能重复的强制检查执行
str.lower()