Taking a vacation: hey, you never know: "days not defined"


#1

I created my own version of this program that works just fine, but I can't get this to work how codeacademy wants me to do it. Every time I try to run it, it says "NameError: days not defined". But isn't "days" just an arbitrary argument in my function? I'm not sure what I'm missing. If I delete my first function, it then says "city is not defined", so this seems to be an issue with all of my arguments.

Thank you in advance for your help!

def hotel_cost(days):
    return 140 * days
print "Hotel cost is %s" % (hotel_cost(days))

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
    else:
        return "Free trip"

print "Plane ticket is %s" % (plane_ride_cost(city))

def rental_car_cost(days):
    cost = int(days) * 40
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost
print "Rental car is %s" % (rental_car_cost(days))

def trip_cost(city, days, spending_money):
   return int(plane_ride_cost(city)) + hotel_cost(days) + rental_car_cost(days) + spending_money

print "Total cost is %s" % (trip_cost(city, days, meals))
trip_cost("Los Angeles", 5, 6)


#2

When you call print "Hotel cost is %s" % (hotel_cost(days)), the function hotel_cost() is expecting an actual variable.


#3

Ok, I switched my code to this, but I guess I'm still missing something:

def hotel_cost(days):
    hotelcost = 140 * int(days)
    return hotelcost
print "Hotel cost is %s" % (hotelcost)

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
    else:
        return "Free trip"

print "Plane ticket is %s" % (plane_ride_cost(city))

def rental_car_cost(days):
    cost = int(days) * 40
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost
print "Rental car is %s" % (rental_car_cost(days))

def trip_cost(city, days, spending_money):
   return int(plane_ride_cost(city)) + hotel_cost(days) + rental_car_cost(days) + spending_money

print "Total cost is %s" % (trip_cost(city, days, meals))
trip_cost("Los Angeles", 5, 6)

#4

I'm still getting the same error as before, except now "hotelcost" is undefined.


#5

Also, all of that stuff is indented normally. It didn't keep the indents when I pasted it.


#6

I've edited your post, but if you want to format code simply place three backticks ( ` ) above and below the code sample (on separate lines).

As for your error, you've made the same mistake again. The print statement is still expecting a value for it to print. I can see what you're trying to do, but hotelcost doesn't "exist" because you haven't actually declared it outside the hotel_cost() function.

Think of it like this:

def myFunction():
    myName = "Nick"
    return myName

print "My name is " + myName

This won't work, because you haven't declared myName anywhere outside of myFunction(). myName is contained only within the scope of the function.

What you would need to do is either declare a myName variable outside of the function, or print the return value of the function itself:

def myFunction():
    myName = "Nick"
    return myName

# Either of the two methods below would work:

print "My name is " + myFunction()

myName = myFunction()
print "My name is " + myName

Take note that the two myName variables are actually different variables. The only difference is that one is in the global scope and the other is within the scope of the function.


#8

Oh ok. That makes sense.

So I changed the first function to this:

def hotel_cost(days):
    hotelcost = 140 * int(days)
    return hotelcost

print "Hotel cost is " + hotel_cost(days)

However, it's having an issue with my argument "days". I'm ultimately trying to run the function trip_cost(city, days, spending_money):

def trip_cost(city, days, spending_money):
   return int(plane_ride_cost(city)) + hotel_cost(days) + rental_car_cost(days) + spending_money

but when I enter in values for my arguments in this function, those argument values don't seem to carry over into my other functions. So when I enter trip_cost("Los Angeles", 5, 600), hotel_cost doesn't seem to understand I've entered in a value for "days". Is that correct?


#9

def hotel_cost(days):
    hotelcost = 140 * int(days)
    return hotelcost

print "Hotel cost is " + hotel_cost(days)

You're still making the same mistake. Python is an interpreted language - that is, it reads the code line by line. If you think of it that way, the print statement makes no sense - what is the days variable in there referring to?

If you want something like that to work, you'd have to actually give it a value. Here are some examples to illustrate what I mean:

def hotel_cost(days):
    hotelcost = 140 * int(days)
    return hotelcost

print "Hotel cost is " + hotel_cost(0)
print "Hotel cost is " + hotel_cost(7)
print "Hotel cost is " + hotel_cost(30)

# The only way in which using days as a variable
# would work is if you actually declare it:

days = 6
print "Hotel cost is " + hotel_cost(days)

When you declare a function (i.e. using def), you can use a placeholder variable like you have with def hotel_cost(days). When you call a function, it expects you to populate the call with a value or declared variable (e.g. days = 7).

At that point in your code days has not been declared in the global scope (where you call the function), hence why you get the error.


Throughout your code you've made similar mistakes to your first - all of your print statements are trying to print variables which do not exist.

The trip_cost function itself is fine - it's the print statement that needs to be fixed. For example:

print "Total cost is %s" % (trip_cost("Los Angeles", 5, 6))

I would also highly recommend that you move your function declarations to the top, before you execute any other line of code.


#10

Ok I finally understand it all, and it now works. Thank you for your patience and great explanation!