# Function Problem

Page 1 of 1

## 5 Replies - 489 Views - Last Post: 03 July 2013 - 02:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324157&amp;s=485406943d2797f3e1bc690ef6153e48&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DblAAssassin

• D.I.C Regular

Reputation: 34
• 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

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

• Pancakes!

Reputation: 8372
• Posts: 14,428
• 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.

### #3 baavgai

• Dreaming Coder

Reputation: 6053
• Posts: 13,106
• 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.

### #4 Nallo

• D.I.C Regular

Reputation: 163
• Posts: 256
• 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)

```

### #5 DblAAssassin

• D.I.C Regular

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

## Re: Function Problem

Posted 03 July 2013 - 02:09 PM

jon.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?

baavgai, 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.

Nallo, 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

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!

### #6 DblAAssassin

• D.I.C Regular

Reputation: 34
• 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

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)
```