Question: what is the wrong with this script

Question

what is the wrong with this script

Answers 1
Added at 2016-12-25 12:12
Tags
Question
def hotel_cost(nights):
    cost = nights * 40
    return cost
def plane_ride_cost(city):
    if city == "charolette":
        cost = 180
    elif city == "los angeles":
        cost = 480
    elif city == "tampa":
        cost = 200
    elif city == "pittspurgh":
        cost = 220
    return cost
def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost

def trip_cost(city, days, spending_money):
    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)\ + spending_money

city_list = ("charolette", "los angeles", "tampa", "pittspurgh")

print "we only support one of those cities" + str(city_list)

city = raw_input (" please choose the city you want to go ")
days = 0
spending_money = 0
if city in city_list:
    days = raw_input("how much days you wanna stay ")
    spending_money = raw_input("how much money do you wanna spend there")
    print trip_cost(city, days, spending_money)
else:
    print 'error'

it always generate this error

rint trip_cost(city, days, spending_money)
  File "/home/tito/test 3 .py", line 23, in trip_cost
    return (hotel_cost(days)) + (plane_ride_cost(city)) + (rental_car_cost(days)) + (spending_money)
TypeError: cannot concatenate 'str' and 'int' objects
Answers
nr: #1 dodano: 2016-12-25 12:12

Converting the user input to the appropriate data types should help:

if city in city_list:
    days = int(raw_input("how many days you wanna stay "))
    spending_money = float(raw_input("how much money do you wanna spend there"))

You always get a string back from raw_input. You can convert a string into an integer with int() or to a float with float(). You will get an ValueError exception, if the string cannot be converter into this data type.

Source Show
◀ Wstecz