Ethan Bacon
Ethan Bacon

Reputation: 243

Subtracting From Int And Printing

I am making a text based game and have been doing just fine but now but I have run into an error with int. My code so far looks like this:

money = 500
print("You Have $500 To Spend On Your City.  The Population Is 0 People")
input_var3 = input("What Will You Spend Your Money On?  A House Or A Restraunt. ")
if input_var3 == "House":
    money - 100
    print("You Have Spent Your Money On A House")
    print("You Now Have $" + money)
if input_var3 == "Restraunt":
    money - 150
    print("You Have Spent Your Money On A Restraunt")
    print("You Now Have $" + money)

Your money is equal to 500 dollars but if you spend it on a house or restraunt you will have less money and the shell will print how much you have left. However I always get this error:

Traceback (most recent call last):
  File "C:\Users\Jason\Desktop\City Text.py", line 11, in <module>
    print("You Now Have $" + money)
TypeError: Can't convert 'int' object to str implicitly

I have realized that I must make a sting instead of the Int but I am not sure how to do this. Can anyone help?

Upvotes: 1

Views: 177

Answers (4)

TerryA
TerryA

Reputation: 59974

The money variable is an integer. You can't mix integers with strings when concatenating them together. Use the str() function, which converts an integer to a string:

print("You Now Have $" + str(money))

Also, I think you're intending to take 100 away from the money value. money - 100 just returns 500 - 100, which is 400. If you want to make money equal to 400, do:

money -= 100

Which is equivalent to:

money = money - 100

Upvotes: 3

Tom Naessens
Tom Naessens

Reputation: 1837

You can use the str() function. For example:

print("You Now Have $" + str(money))

Upvotes: 0

angvillar
angvillar

Reputation: 1094

use:

print("You Now Have $" + str(money))

you can't concatenate a string with a number so convert your number into a string.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80629

You need to store the value inside variable.

if input_var3 == "House":
    money -= 100   # Notice the usage of -=
    print("You Have Spent Your Money On A House")
    print("You Now Have $" + str(money))   # Type casting
if input_var3 == "Restraunt":
    money = money - 150  # Same as -=
    print("You Have Spent Your Money On A Restraunt")
    print("You Now Have $" + str(money))   # Type casting

Upvotes: 1

Related Questions