TypeError: 'str' object is not callable

This is the place for queries that don't fit in any of the other categories.

TypeError: 'str' object is not callable

Postby bluepanido » Thu Jul 11, 2013 12:26 pm

My new friends,
please go easy, as I am learning Python and it's my first language.

I'd be most grateful if someone (anyone!) could help me with this little problem. Here is my code:
Code: Select all
forename = ("Steve",
            "Peter",
            "Jane")
surname = ("Wanstall",
           "O'Connor",
           "Gardiner")

print("Welcome to the name jumble game.")
forint = (input("\nEnter a number from 1-3: "))
surint = (input("\nEnter another number from 1-3: "))

while forint == surint:
    print("The numbers must be different from each other.")
    forint = (input("\nEnter a number from 1-3: "))
    surint = (input("\nEnter another number from 1-3: "))


print("You new name is" (forename[forint - 1]) + (surname[surint - 1]) + ". Hope you like it!")

I get the following error message:

Code: Select all
Traceback (most recent call last):
  File "\\lisa4304\RMTeacherwork\Peter.O'Connor\python\my programs\chapter 4\new ICT name (tuple)", line 18, in <module>
    print("You new ICT name is" (forename[forint - 1]) + (surname[surint - 1]) + ". Hope you like it!")
TypeError: 'str' object is not callable


I've Googled it but all the answers I find are a bit too advanced and beyond me. Can anybody explain what is causing the error and how to fix it? (There is indentation in the code, but it is removed when I copy and paste here).

Thanks in advance for any help.
Peter
Last edited by Yoriz on Thu Jul 11, 2013 4:03 pm, edited 1 time in total.
Reason: added code tags
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK

Re: TypeError: 'str' object is not callable

Postby bluepanido » Thu Jul 11, 2013 4:09 pm

I worked it out thanks, no need to reply thanks.

I wasn't calling the integer variable an integer. It was just a string. Sorry for being such a noob!
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK

Re: TypeError: 'str' object is not callable

Postby ochichinyezaboombwa » Thu Jul 11, 2013 5:04 pm

bluepanido wrote:I wasn't calling the integer variable an integer. It was just a string.

What do you mean? the problem is elsewhere. You can call your variables as you're pleased but you need to learn about functions.

Also: you are using a function already (input), but you are using a bizzarre syntax:
Code: Select all
forint = (input("\nEnter a number from 1-3: "))

The outer ()s are not only unnecessary, putting them there makes your code much harder to read and understand. Just
Code: Select all
forint = input("\nEnter a number from 1-3: ")
is enough.
Again, learn functions.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests