0

这是我正在学习的示例代码。我想获取一个字符串(1 个或多个字符)并将其与现有字符串进行比较。我想输入蓝色,仍然得到答案。如何完全忽略此案?谢谢。

parrot = "Norwegian Blue"

letter = input("Enter a character: ")

if letter in parrot:
    print("The letter {0} is in the Norwegian Blue".format(letter))
else:
    print("Sorry the letter is missing ")

对于成功案例:

C:\PythonMasterClass>python IfProgramFlow.py  
Enter a character: Blue  
The letter Blue is in the Norwegian Blue

对于失败的案例:

C:\PythonMasterClass>python IfProgramFlow.py  
Enter a character: blue  
Sorry the letter is missing

我检查了其他线程以将我的输入和实际文本转换为小写并进行比较,但我想要一种直接的方式。如果我有多个字符串,我不想为此目的将所有字符串都保存为小写。谢谢。

4

1 回答 1

7

尝试:if letter.lower() in parrot.lower():

于 2018-12-04T19:32:00.390 回答