1

I am having a problem using a list that is made in a function that I am iterating. My code at the moment looks like this:

def get_things(i):
    html=str(site[i])
    browser = webdriver.Chrome()  # Optional argument, if not specified will search path.
    browser.get(html);
    playerlist=[]
    teamlist=[]
    all_players = browser.find_elements_by_xpath("//a[@class='name']")
    all_teams = browser.find_elements_by_xpath("//td[@class='last']")
    for a in all_players:
        playerlist.append(str(a.text))
    print playerlist
    for td in all_teams:
        teamlist.append(str(td.text))
    print teamlist
    browser.quit()
    return playerlist, teamlist

I then want to use teamlist and playerlist in another function later in my program.

for i in xrange(0,2):
    get_things(i)
    print teamlist    #This is where Im told teamlist doesn't exist
    print playerlist   #This is where I'm told playerlist doesn't exist
    print_sheet(teamlist, playerlist)

Where the two print statements are for me to make sure that the program is picking up items as it should. My problem, though, is that I am told that teamlist and playerlist do not exist.

NameError: name 'teamlist' is not defined

I believe that the return statement should make these available to the rest of the program, but it is not.

How can I make these lists available to the rest of the program?

2 Answers 2

1

Use the following

(teamlist, playerlist)=get_things(I)

As your function's returning something, you need to handle it.

Example

def add(a,b):
    return a+b
n=add(6,8)
print n #n is 14
Sign up to request clarification or add additional context in comments.

Comments

0

You are need to assign the return to values from get_things to some variables

for i in xrange(0,2):
    playerlist , teamlist = get_things(i) # missing this
    print teamlist    #This is where Im told teamlist doesn't exist
    print playerlist   #This is where I'm told playerlist doesn't exist
    print_sheet(teamlist, playerlist)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.