为什么不,if,elif或者在Python中使用.lower()?

-7 python if-statement lowercase python-3.x

程序需要接受并匹配单词的任何大小写版本,这就是使用.lower()的原因.运行此选项并输入"January"时,将打印else行而不是if行.

month = input("\nPlease enter the month\n")
if month.lower == ("january"):
    month = int(1)
    print(month)
elif month.lower == ("february"):
    month = int(2)
    print(month)
elif month.lower == ("march"):
    month = int(3)
    print(month) #etc.
else:
    print("That is not a month\n")
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 12

你需要调用方法:

month.lower() == 'march'
Run Code Online (Sandbox Code Playgroud)

该方法也是一个对象,如果不调用它,则将该方法与字符串进行比较.他们永远不会平等:

>>> month = 'January'
>>> month.lower
<built-in method lower of str object at 0x100760c30>
>>> month.lower == 'January'
False
>>> month.lower == 'january'
False
>>> month.lower() == 'january'
True
Run Code Online (Sandbox Code Playgroud)