2

Guys I'm a beginner and I'm trying (slightly failing) to teach myself programming and writing code so your help is really appreciated

favorite_foods = {'Armon' : 'wings',
'Dad' : 'kabob',
'Joe' : 'chinese',
'mom' : 'veggies',
'Das' : 'addas_polo',
'Rudy' : 'free_food',
'Nick' : 'hotnspicy',
'layla' : 'fries',
'Shaun' : 'sugar',
'Zareen' : 'cookie',
'Elahe' : 'hotdogs'}

print(favorite_foods)

print "Whose favorite food do you want to know"
person = raw_input()
fav = (favorite_foods[person])

print "%r favorite food is %s" (person, fav)

I keep getting the error:

 TypeError: 'str' object is not callable.

Can you guys tell me whats wrong with my code and how to, for beginners, know what to fix?

Thanks

logic
  • 1,739
  • 3
  • 16
  • 22
Armon Tab
  • 45
  • 4

2 Answers2

6

You are missing % sign here:

print "%r favorite food is %s" % (person, fav)

In your call you have: "%r favorite food is %s" (person, fav), and right after the string object there is a call sign, that's why it thinks that you tried to "call" a string as a function.

You can use the format method:

print "{person} favorite food is {food}".format(person=person, food=fav)
avim
  • 979
  • 10
  • 25
  • 4
    You should also tell the OP about the new preferred `.format()` approach – sshashank124 Apr 19 '15 at 22:05
  • 1
    Note that the statement on `%` formatting [no longer refers to deprecation](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) - see e.g. http://stackoverflow.com/a/13454823/3001761 – jonrsharpe Apr 19 '15 at 22:25
-1

You can also do it like this:

print "{person} favorite food is {food}".format(person=person,food=fav)

I just prefer this way as it is more explicit and it is useful when you have too many parameters to replace in a string to keep track of the order.

dht
  • 41
  • 4