When doing the courses I always try to go into more dept than what’s asked using everything that’s been taught before / stuff I found online. Though it makes for a more enjoyable learning experience (imo) it also leaves me with more questions. See below my final piece of functional code, after which I’ve put all the questions that came to mind.
def hotel_cost(nights): print "Hotel Cost: $%s" %(CostPerNight*nights) return (CostPerNight * nights) def plane_ride_cost(city): if city == "Charlotte" or city =="Ch": flight_cost = 183 elif city in ("Tampa","Ta"): flight_cost = 220 elif city in ("Pittsburgh", "Pb"): flight_cost = 222 return flight_cost elif city in ("Los Angeles", "LA"): flight_cost = 475 else: flight_cost = 0 print "Destionation Entered is not present in database thus flight cost is unknown." print "Destination: %s" %city print "Flight Cost: $%s" %flight_cost return flight_cost def rental_car_cost(days): car_cost = 40 * days if days >= 7: car_cost -= 50 elif days >= 3: car_cost -= 20 print "Car Cost: $%s" % (car_cost) return car_cost def trip_cost(city,days,spending_money): return sum([plane_ride_cost(city), rental_car_cost(days), hotel_cost(days-1),spending_money]) CostPerNight = 140 Location = raw_input("""Where Do You Wanna Go? """) N = int(raw_input("""For how many days do you want to go to %s? """ % (Location))) spend = (N*int(raw_input("""How much money are you willing to spend a day? """))) print "Your Total Budget For Additional Costs is: $%s" % (spend) """This was used before the function total_cost called these same functions plane_ride_cost(Location) rental_car_cost(N) hotel_cost(N-1)""" print "Total Cost: $%s" % (trip_cost(Location,N,spend))
The main ways of how my code differs from what the course asked is that it gives feedback to the console using
After moving some code (Ctrl+X Ctrl+V) I suddenly started typing at two lines at the same time, which I imagine can be useful at times. How do you toggle that?
Initially I had placed the
return of the flight costs within all
if / elif statements, which resulted in the final console feedback after the conditional coding was not being executed. After some testing I noticed that placing
return at the end of the function, instead of within the if statements, solved the issue. Is this because
return ended the function prematurely? Does
return always immediately end a function?
As you can see I used two different ways of checking for the alternative (abbreviated) versions of the same locations. I had found three ways to do this so far,
or (which has been taught in a previous CodeCademy lesson),
in (as used here) and
is. I felt like
in was the most appropriate operator in this as it looks the most ‘neat’ as well as allowing for an easier addition of extra abbreviations. (would you agree? why (not)?).
Something else that feels a bit inefficient is the fact that I got both a
Location variable as well as a
City argument, which are equal as defined. Would there be a way to combine these to use just one name and thus be a bit neater as well as using less NameSpace?
Furthermore both the function
rental_car_cost use the same arguments, why does this not cause any trouble?
I am using
sum to add together all individual costs in
trip_cost, and read that you could use a iterable before the square brackets. What is a iterable and more importantly what does it do in this context? (already tried to learn what’s by searching, I think its use here will make that clearer)
What is the advantage of using the individual functions when finding the
trip_cost as opposed to using some of the variables which are equal to the returned value?
Finally, I have put the variables / the
raw_inputs for the variables below the functions themselves. Is this the ‘correct’ way to do this or should I start putting the variables used in a function above the functions themselves?
I understand that the variables should always be defined before using the functions.
Those are my questions, but please feel free to comment on anything else you feel like could be done better!
A link to the lesson: https://www.codecademy.com/courses/learn-python/lessons/taking-a-vacation/exercises/plan-your-trip?action=resume_content_item note: the suggested solution is quite different from my code and probably will not help you answer my questions. Also not the my code does not pass the SCT (anymore) as it is too different.