Python - taking a vacation part 6 of 7

#1

cant seem to debug it . any help is appreciated

#2

Hard to say without some code of yours

#3

sorry for that , I just uploaded a screenshot . please do check it again

#4

Well you forgot to add Los Angeles as a destination

#5

it doesnât matter . I checked that already . same error

#6

Will need your updated code, could you copy/paste it here? No screenshots.

#7

def hotel_cost(nights):
if nights >= 0:
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 = cost - 50
elif days >= 3 and days < 7 :
cost = cost - 20
return cost
def trip_cost(city, days, spending_money):
sum = rental_car_cost(days) + hotel_cost(days-1) + plane_ride_cost(city) + spending_money
return sum

#8

Alright so I was able to reproduce the error that you had.

Youâre gonna have to put some space in your code. One line of space between each function.

#9

okay I will try it but please explain how will that help? and can you please share you code . I mean I am confused about where to add that space

#10

This is a function, and also a block of code. You want to keep it separated from the other blocks of code.

By having the rest of the code directly attached to the last return, it seems Python tried to interpret the next piece of code as part of this very function, leading to the NoneType error. I could be wrong, but thatâs what seems to have happened there.

Keep a blank line between each function. Thatâs before def, and after the last return.

#11

okay I did that but it still doesnât work , can I copy somebodyâs code, if I can find it, and try it maybe

#12

Iâd say donât give up just yet.

I understand it can be frustrating, but thatâs what programming is. Frustration.
Until you find how to solve the problem and thereâs no better feeling.

I copy/pasted your code, put blank spaces everywhere, and it worked.

Did you add blank lines before and after each block of code? I mean, this, you need to understand, and apply.

You canât just write a huge chunk of code as a big one-block of code. If you ever have to work with other developers, they wonât accept this. Besides, your code probably wonât even work.

Organize your code. You can write, yes? You know how to organize text in paragraphs? Well itâs the exact same concept, applied in your code

``````def some_function:
#some code here
#return the result

def other_function:
#some code here
#return the result
``````

You need to organize your code clearly.

#13

thank you for the help ghost . it works now but I had to edit the line 19 from

elif days >= 3 and days < 7 :
to
elif days > =3 :

and now it passed the test . what I need to ask is , was my code wrong before? because I donât think so . lets say the value entered by user was 2 for days , now going back to function
def car_rental_cost(days) , in which days = 2 . it will execute the line below this which is cost = days * 40 i.e 2 * 40 = 80 . now it will move further down to âifâ statement and since days isnât greater than 7 , it will just skip it and then it will skip âelifâ too as 2 doesnât lie between 3 and 7 . so it will just return the default cost which is 2*40 or the first cost just below the function. I hope you understand what I am trying to convey here . English isnât my first language so it might me a problem.

hereâs the screen shot of the new code and thank you for all the help

#14

If there are only 2 days, then yes it wonât take into account the price reduction offer.

Itâs basically saying âYou want to rent a car? Great! Look, if you rent for seven or more days, you get a 50 discount on the price. If you rent for 3 or more days, you get a 20 discount.â

So if you rent for less, regular pricing applies.

You actually didnât need `days < 7`, so good job for spotting that one.

You can still improve your code some more.

``````cost = cost - 50
cost -= 50
``````
``````cost = cost - 20
cost -= 20
``````
``````sum = rental_car_cost(days) + hotel_cost(days-1) + plane_ride_cost(city) + spending_money
return sum

return rental_car_cost(days) + hotel_cost(days-1) + plane_ride_cost(city) + spending_money
``````

You also donât need a space before the colons in your if / elif statement:

`````` if days >= 7:
elif days >= 3:
``````

but thatâs not as important.

But well done, do you see how cleaner your code looks now? Itâs much simpler to get through.

#15

I actually code a lot in c so I am used to inserting spaces everywhere I can , I am working on it . anyway it was nice talking to you and finally I am moving on to the next topic

thank you again for all the help

#16

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