2015-09-26 167 views
4

我在python 3中制作了一个简单的疯狂libs程序,用户输入名词和代词,程序应该打印出用户的输入。TypeError:无法将'builtin_function_or_method'对象隐式转换为str

这里是我的代码:

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.") 

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

在此行中:

print (pronoun1 + " saw a " + noun2 + ".") 

相当新的蟒蛇,所以我不能完全肯定什么这个错误的意思和如何解决它,有人可以解释这个错误代码给我吗?

回答

5

的问题是与名2变量

noun2 = input("Saw a ________ (Noun).").lower 

您分配功能.lower到它,而不是调用它的结果。你应该叫()函数作为.lower -

noun2 = input("Saw a ________ (Noun).").lower() 

对于未来的读者

,当你得到一个问题,比如 - 试图串联变量包含使用+操作字符串时 - TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

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

这(如与OP的情况下),当您试图呼吁串一些功能,但它错过了()语法(如发生在OP的情况下)通常发生 -

name = name.lower #It should have been `name.lower()` 

如果没有()语法,您只需将函数/方法分配给变量,而不是调用该函数的实际结果。为了调试这些问题,你可以查看你分配给变量的行,在引发错误的行中使用,并检查是否错过了调用任何函数。

+0

哇,完全飞过我,谢谢你指出! –

相关问题