Python - taking a vacation part 6 of 7


#1

cant seem to debug it . any help is appreciated :slight_smile:


#2

Hard to say without some code of yours :wink:


#3

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


#4

Well you forgot to add Los Angeles as a destination


#5

it doesn’t matter . I checked that already . same error :frowning:


#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.

First it will help with code readability. Second, it will help you avoid errors.

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 :slight_smile:

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.

Please show the latest version of your code + whatever error you’re getting.


#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 :slight_smile:


#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 :slight_smile:

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.