TypeError:无法隐式地将'builtin_function_or_method'对象转换为str

W. *_*med 4 python string typeerror python-3.x

我在python 3中制作了一个简单的疯狂libs程序,用户输入名词和代词,程序应打印出来自用户的输入.

这是我的代码:

print ("Welcome to Mad Libs. Please enter a word to fit in the empty space.")

proper_noun = input("One day _________ (Proper Noun)").lower()
ing_verb = input("Was __________ (Verb + ing) to the").lower()
noun1= input("to the _________ (Noun)").lower()
pronoun1 = input("On the way, _____________ (Pronoun)").lower()
noun2 = input("Saw a ________ (Noun).").lower
pronoun2 = input("This was a surprise so ________ (Pronoun)").lower()
verb2 = input("_________ (verb) quickly.").lower()
#Asks user to complete the mad libs

print ("One day " + proper_noun)
print ("Was " + ing_verb + " to the")
print (noun1 + ". " + "On the way,")
print (pronoun1 + " saw a " + noun2 + ".")
print ("This was a surprise")
print ("So " + pronoun2 + " " + verb2 + " quickly.")
Run Code Online (Sandbox Code Playgroud)

获取此错误代码: TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

在这一行:

print (pronoun1 + " saw a " + noun2 + ".")
Run Code Online (Sandbox Code Playgroud)

相当新的python,所以我不完全确定这个错误意味着什么以及如何解决它,有人可以向我解释这个错误代码吗?

Ana*_*mar 6

问题在于noun2变量

noun2 = input("Saw a ________ (Noun).").lower
Run Code Online (Sandbox Code Playgroud)

您正在为它分配函数.lower,而不是调用它的结果.你应该把这个函数称为.lower() -

noun2 = input("Saw a ________ (Noun).").lower()
Run Code Online (Sandbox Code Playgroud)

对于未来的读者

当你遇到诸如以下的问题TypeError: Can't convert 'builtin_function_or_method' object to str implicitly时 - 当尝试连接变量包含使用+运算符的字符串时.

问题基本上是其中一个变量实际上是一个函数/方法,而不是实际的字符串.

当你尝试在字符串上调用某个函数时,通常会发生这种情况(如OP的情况),但是错过了()它的语法(如OP的情况) -

name = name.lower   #It should have been `name.lower()`
Run Code Online (Sandbox Code Playgroud)

如果没有()语法,您只需将函数/方法分配给变量,而不是调用函数的实际结果.要调试此类问题,您可以查看分配给变量的行,在抛出错误的行中使用,并检查是否错过了调用任何函数.