为什么 if、elif 或 else 不能与 Python 中的 .lower() 一起使用?
Why doesn't if, elif or else work with .lower() in Python?
程序需要接受和匹配单词的任何大写和小写版本,这就是使用 .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")
您需要调用方法:
month.lower() == 'march'
该方法也是一个对象,如果不调用它,您就是将该方法与字符串进行比较。他们永远不会相等:
>>> 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
程序需要接受和匹配单词的任何大写和小写版本,这就是使用 .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")
您需要调用方法:
month.lower() == 'march'
该方法也是一个对象,如果不调用它,您就是将该方法与字符串进行比较。他们永远不会相等:
>>> 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