Miguel Nunez
Miguel Nunez

Reputation: 11

Python: cannot concatenate 'str' and 'int' objects error

I'm trying to create a little program that lets the user to buy stuff from the shop or money buy working at a job.

Code:

#Info before user starts

print "Enter job, shop, or exit"
print ""

#--------------------------------------------------------------------------------------------
#Variabls

name = raw_input("What is your name?")
ask = raw_input("Where do you want to go:")
currency = 20

#--------------------------------------------------------------------------------------------
#Functions

def job():
  print "hello"

def shop():
  print "Hello " + name + ", what would you like? You have $" + currency

#-------------------------------------------------------------------------------------------
#Body

while (ask != "job") and (ask != "shop") and (ask != "exit"):
  print "That is not an option. Please choose job, shop, or exit"
  ask = raw_input("Where do you want to go:")

if(ask == "job"):
  job()
elif (ask == "shop"):
  shop()

The programs asks the user's name and asks where he would like to go. For the function shop, the program should print: "Hi [User's name], What would you like? You have $20". When I run it, it shows up this error:

Traceback (most recent call last):
  File "python", line 30, in <module>
  File "python", line 18, in shop
TypeError: cannot concatenate 'str' and 'int' objects

Could anyone explain what is happening?

Upvotes: 1

Views: 8052

Answers (3)

Algorithmic Canary
Algorithmic Canary

Reputation: 742

Python takes a strict view towards types and doesn't convert between type implicitly like dynamic languages do. If you want your numbers to become strings, you must explicitly convert to string with the str function. This is part of the Zen of Python:

Explicit is better than implicit.

By requiring the programmer to explicitly convert between types there removes some surprises where numbers or strings get added. For example, it is not immediately obvious if 2 + 3 + "foo" should equal "23foo" or "5foo"

There are sometimes where you don't have to explicitly convert to a string, for example in print statements, numbers will automatically be converted to strings if they are the only thing in the statement. However, if you attempt to add the number to a string before hand passing it to the print statement, then you have to explicitly convert to a string.

If your case, you want to say

print "Hello " + name + ", what would you like? You have $" + str(currency)

Upvotes: 0

Sebasthian Ogalde
Sebasthian Ogalde

Reputation: 517

The '+' operator has its own behaviour for some variable types (or object class, to be more precise). This is called operator overloading. In the case of adding two integers, the result is the obvious:

a = 1
b = 5
print(a+b)
   Out[0]: 6

In the other side, when you try to add two strings, Python understands it as concatenation. So:

a = 'hi '
b = 'my friend'
print(a+b)
    Out[0]: 'hi my friend'

In your case, you are trying to add a string and an integer. Python does not have any idea how to add these, or more precisely, the '+' operator is not defined when adding objects 'str' and 'int'. In order to fix your code, you need to cast the variable currency to string:

def shop():
  print "Hello " + name + ", what would you like? You have $" + str(currency)

Upvotes: 0

sali333
sali333

Reputation: 115

use the str function in order to convert "currency" to a string

def shop():
      print "Hello " + name + ", what would you like? You have $" + str(currency)

Upvotes: 1

Related Questions