Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Source 

python code

def hotel_cost(nights):
return nights*140

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):
return days*40
if days>=7:
return days*40 - 50
elif days>= 3 and days < 7:
return days*40 - 20





#is anything wrong in the code above i am learing python at codeacademy and the result says that above code return value 120 instead of 100 for days = 3
#and also please tell me what are indentational errors
In your rental_car_cost(), you return days*40 before anything else happens. When you return that value, the code doesn't continue you down after that. Take return days*40 and put it at the bottom of your function.
[Image: CIbBKha.gif]
(Jan 25, 2017 08:19 AM).russo Wrote:  In your rental_car_cost(), you return days*40 before anything else happens. When you return that value, the code doesn't continue you down after that. Take return days*40 and put it at the bottom of your function.

you mean that if we use return with the parameter like (days) in this case it just
returns the value of the function and breaks their if this is the case i should create a new variable called cost and replace it with all return in that function and then just return cost at the end
In your code, for this part:

Code
def rental_car_cost(days):
return days*40
if days>=7:
return days*40 - 50
elif days>= 3 and days < 7:
return days*40 - 20

You don't need/should have this line:
Code
return days*40

Rather if I understand what you're trying to do; It should be:
Code
def rental_car_cost(days):
if days>=7:
return days*40 - 50
elif days>= 3 and days < 7:
return days*40 - 20
else:
return days*40
thanks i got your point !!
(Jan 27, 2017 10:42 AM)Annabelle Lee Wrote:  In your code, for this part:

Code
def rental_car_cost(days):
return days*40
if days>=7:
return days*40 - 50
elif days>= 3 and days < 7:
return days*40 - 20

You don't need/should have this line:
Code
return days*40

Rather if I understand what you're trying to do; It should be:
Code
def rental_car_cost(days):
if days>=7:
return days*40 - 50
elif days>= 3 and days < 7:
return days*40 - 20
else:
return days*40

No need for elif/else at all. He's returning in earlier if clauses, so if the condition is met they won't be reached anyway.
(Jan 27, 2017 05:11 PM)Dialog Wrote:  No need for elif/else at all. He's returning in earlier if clauses, so if the condition is met they won't be reached anyway.

I was simply editing his code to be as little changes as possible, but yeah you have a point.