1

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?

2
  • You can't add a string to the number 20. Make currency a string with print "Hello " + name + ", what would you like? You have $" + str(currency) Commented Oct 22, 2017 at 1:37
  • You could use format to output the int and other variable. eg. print("Hello {} , what would you like? You have $ {} ".format(name, currency)) Commented Oct 22, 2017 at 1:50

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. That worked. Why does Python do that though? I just moved from JavaScript and it would take a number and convert to a string, but python doesn't
Or quit adding to a string and use string formatting instead. "Hello {0}, what would you like? You have ${1}".format(name, currency)
@MiguelNunez Javascript is stupid about type safety. Ask yourself: should "1" + 1 give "11" instead of 2? What if it's 1 + "1"?
I've always considered javascript as a strange programing language .
@MiguelNunez although if you're just starting out in Python, go get Python3. It's been out for over a decade and is the de facto standard. Don't start any new projects in Py2.
|
0

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)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.