2

I am trying to learn python using codeacdemy. This was one of their excercises. Basically they had me create 4 different functions that calculated the total cost. But there was no option to ask the user to manually enter in the values. So thats what I am trying to to. The code is right upto the return rental-car_cost part. its just the bottom bit where I am having trouble.

print "this code calculates the total price of a trip, using 4 functions"

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

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 rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money

city= raw_input("enter city name")
days= raw_input("enter number of days staying")
spending_money= raw_input("enter spendig money")
print trip_cost(city,days, spending_money)

this was the original code and it rune perfectly fine. All i want to do is have the user enter the values when the code it running.

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

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 rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money

print trip_cost("Los Angeles",5,600)
8
  • Btw I know i can just say print trip_cost("los angeles", 5, 100) and that will give me the answer. But i want to ask the user in the console without changing the code every time. Thanks for the help Commented Sep 27, 2014 at 1:29
  • 1
    You need to fix your indentation. Commented Sep 27, 2014 at 1:33
  • i think my indentation is right for the original code. my code runs error free, and i know is 100% right upto return rental_car_cost(days) +hotel-cost(days)+..... Commented Sep 27, 2014 at 1:41
  • As it appeared here it was not. I've fixed it. Did you try to run the code as it was originally? As it was it would not even have compiled. Commented Sep 27, 2014 at 1:44
  • Is the problem that the code does not run or gives incorrect results? I believe you will need to cast the output of the raw_input calls to float to get the days and spending money input. Commented Sep 27, 2014 at 2:03

2 Answers 2

1

Equal question:

Vacation price program Python

Propose

Consider it only like some to improve this code. I think so it didn't answer your question.

I don't know what propose of Code Academy for that exercise but some way easier and cleaner is at below:

print "this code calculates the total price of a trip, using 4 functions"

def hotel_cost(nights):
    return 140 * nights

def plane_ride_cost(city):
    #So you can create dict and put for each city
    #Key - name of city
    #value - cost
    CITY_COST = {
        "Charlotte": 183,
        "Pittsburgh" : 222,
        "Los Angeles" : 475,
        "Tampa": "220"
    }
    #Method from dict 
    #if city doesn't exists it'll return False
    #The second param is default return if doesn't exist key into dict
    #you can change if do you want
    return CITY_COST.get(city, False)



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 rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money

city= raw_input("enter city name")
days= raw_input("enter number of days staying")
spending_money= raw_input("enter spendig money")
print trip_cost(city,days, spending_money)

Documentation about Dict

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Sign up to request clarification or add additional context in comments.

1 Comment

this code has the exact same problem as the code that i wrote :(. I cant figure out what i am doing wrong
0

Try using int(raw_input(enter number of days staying")) or input("enter number of days staying") instead of raw_input("enter number of days staying") What happens ? You see any difference ? This is because raw_input() converts input data into string but it's not the same with input(). Find out the differences between input() and raw_input() and how it has changed as python has evolved. I have made some changes to the code as shown below. It runs perfectly without errors. Let me know if it helped you.

print "this code calculates the total price of a trip, using 4 functions"

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

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 rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money

city= raw_input("enter city name")
days= int(raw_input("enter number of days staying"))  ##### notice here
spending_money= int(raw_input("enter spendig money")) ### and here too
print trip_cost(city,days, spending_money)

Instead of the above you can use the following code too.

################## without type casting#############
    city= raw_input("enter city name")
    days= input("enter number of days staying")  ##### notice something here
    spending_money= input("enter spendig money") ### and here 
    print trip_cost(city,days, spending_money)

1 Comment

thx so much I understand now. the code works perfectly and runs well too. Thanks!!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.