5 Replies - 440 Views - Last Post: 03 July 2013 - 02:21 PM Rate Topic: -----

#1 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 261
  • Joined: 11-May 13

Function Problem

Posted 03 July 2013 - 09:50 AM

Let me start with saying, I doing the Python Tutorial on Codecademy. Great Website BTW.

I'm on this lesson. http://www.codecadem...788890003000096

This is the code...
def hotel_cost(nights):
    total = nights * 140
    return total

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 "That is not an Option!"

def rental_car_cost(days):
    cost = days * 40
    discount1 = days * 40 - 20
    discount2 = days * 40 - 50
    if days <= 2:
        return cost
    elif days >= 3 and days <= 6:
        return discount1
    elif days >= 7:
        return discount2
    
def trip_cost(city, days, spending_money):
     return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

trip_cost(city, days, spending_money)
print trip_cost



This is the Error...

Quote

Traceback (most recent call last):
File "python", line 31, in <module>
NameError: name 'city' is not defined


I don't understand because City is defined, in the second function! Any help would be appreciated, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Function Problem

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Function Problem

Posted 03 July 2013 - 10:24 AM

city is not defined on line 31. The definition here
def plane_ride_cost(city):



is local to that function. You should read up on scope.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Function Problem

Posted 03 July 2013 - 11:21 AM

In programming, only calculate once, if you can. And try not to repeat yourself.

Your function could be written as:
def rental_car_cost(days):
	cost = days * 40
	if days < 3:
		return cost
	elif days < 7:
		return cost - 20
	else:
		return cost - 50




Sorry, it was bugging me.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Function Problem

Posted 03 July 2013 - 01:52 PM

You need to give the toplevel variables city, days and spending_cost a value before you call the trip_cost function.

To clarify, a toy example that doesn't work (same error than yours):
def celsius(fahrenheit):
    return (fahrenheit - 32) * 5.0 / 9.0

print celsius(fahrenheit)



And a version that works:
def celsius(fahrenheit):
    return (fahrenheit - 32) * 5.0 / 9.0

fahrenheit = 55 #give it a value before you use it!
print celsius(fahrenheit)


Was This Post Helpful? 0
  • +
  • -

#5 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 261
  • Joined: 11-May 13

Re: Function Problem

Posted 03 July 2013 - 02:09 PM

View Postjon.kiparsky, on 03 July 2013 - 10:24 AM, said:

city is not defined on line 31. The definition here
def plane_ride_cost(city):



is local to that function. You should read up on scope.


So your saying I need to define it again?

View Postbaavgai, on 03 July 2013 - 11:21 AM, said:

In programming, only calculate once, if you can. And try not to repeat yourself.

Your function could be written as:
def rental_car_cost(days):
	cost = days * 40
	if days < 3:
		return cost
	elif days < 7:
		return cost - 20
	else:
		return cost - 50




Sorry, it was bugging me.

Hope this helps.


I was following the parameters, of the situation, your functions. Doesn't follow the requirements.

View PostNallo, on 03 July 2013 - 01:52 PM, said:

You need to give the toplevel variables city, days and spending_cost a value before you call the trip_cost function.

To clarify, a toy example that doesn't work (same error than yours):
def celsius(fahrenheit):
    return (fahrenheit - 32) * 5.0 / 9.0

print celsius(fahrenheit)



And a version that works:
def celsius(fahrenheit):
    return (fahrenheit - 32) * 5.0 / 9.0

fahrenheit = 55 #give it a value before you use it!
print celsius(fahrenheit)



I tried, doing this in my code, then this happened.
def hotel_cost(nights):
    total = nights * 140
    return total

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 "That is not an Option!"

def rental_car_cost(days):
    cost = days * 40
    discount1 = days * 40 - 20
    discount2 = days * 40 - 50
    if days <= 2:
        return cost
    elif days >= 3 and days <= 6:
        return discount1
    elif days >= 7:
        return discount2
    
def trip_cost(city, days, spending_money):
     return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

hotel_cost(nights) = 5
plane_ride_cost(city) = "Los Angeles"
rental_car_cost(days) = 5
trip_cost(city, days, spending_money)
print trip_cost



The New Error reads...

Quote

Traceback (most recent call last):
File "runner.py", line 125, in compilecode
File "python", line 31
SyntaxError: can't assign to function call


So, I think I got the original problem solved, now a new one has arisen!! Once, again any help will be appreciated! :)
Was This Post Helpful? 0
  • +
  • -

#6 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 261
  • Joined: 11-May 13

Re: Function Problem

Posted 03 July 2013 - 02:21 PM

Disregard my Last, I found it out, after, I paused, and thought, and remembered, how to do this. Anyways thanks to everyone that helped the final code is here!
def hotel_cost(nights):
    total = nights * 140
    return total

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 "That is not an Option!"

def rental_car_cost(days):
    cost = days * 40
    discount1 = days * 40 - 20
    discount2 = days * 40 - 50
    if days <= 2:
        return cost
    elif days >= 3 and days <= 6:
        return discount1
    elif days >= 7:
        return discount2
    
def trip_cost(city, days, spending_money):
     return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

print trip_cost("Los Angeles", 5, 600)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1