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)
-
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 helpuser4081147– user40811472014年09月27日 01:29:40 +00:00Commented Sep 27, 2014 at 1:29
-
1You need to fix your indentation.Crowman– Crowman2014年09月27日 01:33:15 +00:00Commented 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)+.....user4081147– user40811472014年09月27日 01:41:39 +00:00Commented 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.Joe McMahon– Joe McMahon2014年09月27日 01:44:41 +00:00Commented 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.Lukeclh– Lukeclh2014年09月27日 02:03:54 +00:00Commented Sep 27, 2014 at 2:03
2 Answers 2
Equal question:
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
1 Comment
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)