Taking a vacation


#1



I wanted to add some features to the final code - to make it so, that you could enter a number of days to stay, city and money to spend from keyboard, rather then use fixed values. I thought my previous learning from the course would be enough for this, but apparently, I got stuck.


Traceback (most recent call last):
File "python", line 35, in
File "python", line 28, in trip_cost
File "python", line 20, in rental_car_cost
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'


Instead, I expect to enter 3 values from keyboard, and get a calculated total sum to spend


def hotel_cost(nights):
    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
    else:
        print "Choose a city we can provide a flight to"

def rental_car_cost (days):
    if days < 3:
        return 40 * days
    elif days > 6:
        return 40 * days - 50
    else:
        return 40 * days - 20

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

n = input("Enter a number of days you'd like to stay in a hotel: ")

m = raw_input("Enter a city you'd like to fly to: ")

p = input("Enter an ammount of cash you'd like to spend on your vacation: ")
print trip_cost(n, m, p)


#2

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

n = input("Enter a number of days you'd like to stay in a hotel: ")
m = raw_input("Enter a city you'd like to fly to: ")
p = input("Enter an ammount of cash you'd like to spend on your vacation: ")
print trip_cost(m, n, p) #city is the first... def trip_cost (city, days, spending_money):

#3

Thank you very much. Now I am ashamed of making such a silly mistake :frowning:


#4

Have you zeroed in on what is causing this error?


#5

I was helped by a tutor in this topic. He corrected it, using #comment. I just put arguments for trip_cost function (the last line) in the wrong order - m should be the first, then n and then p.


#6

Are you aware of the difference between input and raw_input?


#7

I understand it so, that raw_input is used, when it is expected to receive "string" type of data from keyboard, e.g. name of the city in this particular case. Input is used when you expect a number. Am I correct?


#8

Haha @oldnara I'm glad I could help. I'm not a tutor, just a learner like you.


#9

Sorry, thought that just members of the stuff can #comment. Don't know why, actually :slight_smile: Anyway, thanks a lot.


#10

You're welcome. :+1:


#11

For our purposes, raw_input is the function to reach for, string or number. input returns Unicode which needs to be evaluated.

raw_input()

is that evaluation (in Python 2.x).

When a number is expected, we convert the return value to an integer.

num = int(raw_input('Enter a number'))

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.