Hack Forums
[Source] python code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Source] python code (/showthread.php?tid=5533332)



python code - hesienberg3031 - Jan 25, 2017

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


* - .russo - Jan 25, 2017

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.


* - hesienberg3031 - Jan 27, 2017

(Jan 25, 2017 08:19 AM)☯ 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


* - Annabelle Lee - Jan 27, 2017

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



* - hesienberg3031 - Jan 27, 2017

thanks i got your point !!


* - Dialog - Jan 27, 2017

(Jan 27, 2017 10:42 AM)AnnabelleLee 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.


* - Annabelle Lee - Jan 27, 2017

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