# Problem defining variables in main program from a function

Page 1 of 1

## 10 Replies - 620 Views - Last Post: 10 October 2012 - 05:13 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=295120&amp;s=8b2cf37682baa0bd2d999dda257035b3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

# Problem defining variables in main program from a function

Posted 10 October 2012 - 03:33 PM

```def findLocation(yearList, locationList, year):

i = 0
year = 0
getYear = input("What is the year of the Olympics you are looking for? ")

while i < len(yearList) and year == 0:
if yearList[i] == year:
found = 1
else:
i = i + 1
if found == 1:
print("The location of the Olympics in the year", year, "you chose was in", locationList[i])
else:
print("There is no location for that year")

#Main Program

findLocation(yearList, locationList, year)
```

I always get this problem where it says 'year' is not defined but I thought i defined it in my findLocation function. I know I need to define year in my main program but I dont know how to because I thought I already called it in my function.

Is This A Good Question/Topic? 0

## Replies To: Problem defining variables in main program from a function

### #2 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 03:49 PM

Variables you declare within a function are local to that function, and can't be used outside the function. This is known as a local variable. You must define the values the local variables will take before you call the function.

To use values in a function call, you need to declare (or pass in) actual parameters first. These are the values that "yearList", "locationList" and "year" will take. The parameter "year" will be assigned to the variable "year" inside the function

```# Something like this
yearList = [1, 2, 3]
locationList = ["x", "y", "z"]
year = 1990
findLocation(yearList, locationList, year)

```

This post has been edited by Simown: 10 October 2012 - 03:50 PM

### #3 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 03:53 PM

Hello:

Function parameters only exist during the function call. Since you are prompting for a year within the function, it shouldn't be parameter at all:

```def findLocation(yearList, locationList):

i = 0
found = False
year = int(input("What is the year of the Olympics you are looking for? "))

if yearList[i] == year:
found = True
else:
i = i + 1
if found:
print("The location of the Olympics in the year", year, "you chose was in", locationList[i])
else:
print("There is no location for that year")

#Main Program

yl = [2000,2004,2008]
ll = ["France","China","Antartica"]

findLocation(yl, ll)
```

Note, the variable I used in main have different names; they are bound to yearList and locationList when the function is called. Hope this helps.

### #4 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 03:55 PM

Thank you...I think i figured it out now that after i figure out what the year is, I set a value to that spot in the list and then return it?

### #5 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 04:10 PM

jlessard93, on 10 October 2012 - 03:55 PM, said:

Thank you...I think i figured it out now that after i figure out what the year is, I set a value to that spot in the list and then return it?

I made a couple changes to your code when I re-posted, including the while loop. If yours is set up like what I posted, you can just tack on a 'return locationList[i]' at the end of the function. Otherwise, you could put the return statement within the loop.

This post has been edited by NathanMullenax: 10 October 2012 - 05:38 PM

### #6 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 04:39 PM

My problem mainly is that I can get the program to work...but I am supposed to have year in the function getLocation like
``` def findLocation(yearList, locationList, year)
```

What i dont understand is where I define year...you may have answered this question but I am really stuck on this I am sorry

### #7 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 04:46 PM

```# Olympics Results

# Given a file with where the summer olympics were held since 1896

def getOlympics():
# This function will get the data from the data file - be sure to look at the format of the data in the
# file and read each line as we did with the phone search program in class.
# The function should return the list of years, the list of winners and the list of losers

fname = input("Enter the name of the data file: ")
infile = open(fname,'r')
yearList = []
locationList = []

# Get the first line in the file
# As long as the line from the file is not empty, keep reading
while line != "":
# Strip off end-of-line character
line = line.strip()
# Split the two items on the list separated by a comma
year, location = line.split('\t')
# Insert the values into the lists
yearList = yearList + [year]
locationList = locationList + [location]

# Get the next line from the file
#Close file
infile.close()

return yearList, locationList

def findLocation(yearList, locationList, year):
# This function will take the year list and the location list
# The function should ask the user for the year and print the location
# or an error message if there is no location for that year
i = 0
year = 0
getYear = input("What is the year of the Olympics you are looking for? ")

while i < len(yearList) and year == 0:
if yearList[i] == year:
found = 1
year = yearList[i]
else:
i = i + 1
if found == 1:
location = print("The location of the Olympics in the year", year, "you chose was in", locationList[i])
else:
print("There is no location for that year")
return location

#Main Program
yearList, locationList = getOlympics()

findLocation(yearList, locationList, year)
```

this is what I have so far yet this doesn't work

### #8 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 04:55 PM

Maybe they don't want you to prompt for a year inside the function, but rather in main. It would be hard to say without seeing the assignment.

### #9 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 05:03 PM

you should have the function take as parameters
the list of years, the list of locations and the year that you are searching for, and
it should return the location for that year. So the function will look like this::
def findLocation(yearList, locList, year):
# Fill in the code to find the year
# in the year list and return the
# associated location from the location list
return location

this is what the assignment is telling me what to do...sorry about the &'s

This post has been edited by atraub: 10 October 2012 - 09:38 PM
Reason for edit:: Fixed those irritating &'s

### #10 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 05:10 PM

Yeah, it sounds like findLocation is just for querying the data arrays, not user input, so you should probably get rid of all of the print and input statements, and just return the location.

Another thing to note: something like this

```s = print("Whatever")
```

Will set s to NoneType, whereas

```s = "Whatever"
```

Will actually set the string.

### #11 jlessard93

Reputation: 1
• Posts: 11
• Joined: 08-October 12

## Re: Problem defining variables in main program from a function

Posted 10 October 2012 - 05:13 PM

Thank you very much for your help tonight sir!!