Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

I'm making a simple mad libs program in python 3 where the user enters in nouns and pronouns and the program is supposed to print out the inputs from the user.

Here is my code:

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

Getting this error code: TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

On this line:

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

Fairly new to python, so i'm not entirely sure what this error means and how to fix it, can somebody explain this error code to me please?

like image 522
W. Ahmed Avatar asked Sep 26 '15 05:09

W. Ahmed


1 Answers

The issue is with the noun2 variable

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

You are assigning the function .lower to it , not the result of calling it . You should call the function as .lower() -

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

For Future readers

when you get an issues such as - TypeError: Can't convert 'builtin_function_or_method' object to str implicitly - when trying to concatenate variables contain strings using + operator.

The issue basically is that one of the variables is actually a function/method, instead of actual string.

This (as in the case with OP) normally happens when you try to call some function on the string, but miss out the () syntax from it (as happened in the case of OP) -

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

Without the () syntax, you would be simply assigning the function/method to the variable , and not the actual result of calling the function. To debug such issues you can check out the lines where you are assigning to the variables , used in the line throwing the error, and check if you missed out on calling any function.

like image 85
Anand S Kumar Avatar answered Sep 22 '22 12:09

Anand S Kumar