Reputation: 31
I'm still very much a beginner with Python and I'm using codecademy at the minute. I decided to complicate one of the practice programs slightly and I've come across this error:
Traceback (most recent call last):
File "python", line 30, in <module>
File "python", line 24, in trip_cost
File "python", line 18, in rental_car_cost
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'
Here is my code:
def hotel_cost(nights):
# the hotel costs $140 per night
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rental_car_cost(days):
cost = days * 40
if days >= 7:
cost -= 50
elif days >= 3 and days < 7:
cost -= 20
return cost
def trip_cost(city, days, spending_money):
return rental_car_cost(days) + hotel_cost(days - 1) + plane_ride_cost(city) + spending_money
x = raw_input("Where are you going? ")
y = raw_input("How many days are you going for? ")
z = raw_input("How much spending money are you taking? ")
print trip_cost(x, y, z)
Any help would be much appreciated, thanks! I'm a beginner so my python lingo is a bit rusty.
Upvotes: 0
Views: 1011
Reputation: 11942
You're doing days - 1
or cost -= 50
but days
and cost
are user inputs and therefore a string (unicode string more accurately in your case), you should convert to int
before doing anything (especially math) since that's your intention:
y = int(raw_input("How many days are you going for? "))
z = int(raw_input("How much spending money are you taking? "))
This is because you are using them as if they're numbers without converting
However be warned, inputting in anything other then numbers will generate errors, but that's the subject for another question
Upvotes: 3