Python4.Transportation


#1

def hotel_cost(nights):
    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):
    rental_car_cost = 40 * days
if days >= 7:
    rental_car_cost - 50
elif days >= 3 and days < 7:
    rental_car_cost - 20
    print rental_car_cost

this is my code what have I done wrong?? It keeps on saying that 'days' is not defined?? Urgent help please!!!


#2

Everything was fine until I noticed this...

Can You figure out what you missed?
I give ya hint:
Function either prints or returns or updates a value (in case of conditional statements) after it process its arguments.

If you wanted to just update the value of rental_car_cost to use this value end of the block,
Then simply you missed something? can u guess?

I hope it helps! :slight_smile:


#3

thnxs for that hint but I still cant understand


#4

Ok I will show one part of the code

def rental_car_cost(days):
    if days >= 7:
        return days * 40 - 50

If I use your way (defining variable at first then update it)

def rental_car_cost(days):
    rental_car_cost = 40 * days
if days >= 7:
    rental_car_cost = rental_car_cost - 50 # Im re-assigning the value again

But in this case at the end of the block I will use return statement.

return rental_car_cost

Does it give ya more clarity ?
Another thing to note you're using same name for function and the variable inside it.
Which is bad coding practice, when the code gets bigger you will mix it up and make mistake.


#5

yes thank you so very much


#6

Youre welcome !:slight_smile:


#9

This worked for me, but I'd love to know if it could have been written better

Yah! There are many ways to make this code better.
In general (not only this code but any)
We can make it more effective by
1. by reducing the execution time
2. by design (documentation and styles)

I'd suggest you to go with your methods. Don't look for perfection instantly.
Reading--
PEP 8 -- Style Guide for Python Code
PEP 20 -- The Zen of Python [Be sure you read it!]

Best coding practices in python-
Effective Python by Brett Slatkin (But don't try it until your fundamentals are clear!)

You'll become better day by day, just don't forget to code every day. :slight_smile:

I know this is not answer of your exact question but this will give ya a wide overview. I hope it helps. :thumbsup:


#10

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