0

I apologize if this is a bit basic, but I'm mostly a JavaScript developer so I'm unfamiliar with the way Python interprets code. It looks like I'm running into some sort of async/hoisting error on the following code:

if resultsLength == 10:
    listLength = df.head(10).iterrows()
elif resultsLength == 15:
    listLength = df.head(15).iterrows()
elif resultsLength == 20:
    listLength = df.head(20).iterrows()
elif resultsLength == 25:
    listLength = df.head(25).iterrows()

for index, row in listLength:
    .../do stuff

The error I'm getting is this:

UnboundLocalError: local variable 'listLength' referenced before assignment

How do I deal with asynchronous issues like this in Python? Am I supposed to define the for loop as a function and use it as a callback?

solution:

Alex Hall was correct, my if test was failing because I forgot to convert resultsLength to an int, like so

if int(resultsLength) == 10:

For the record, I really shouldn't be getting downvotes for asking a "stupid" question if the majority of people are upvoting the wrong answer. Thanks, Alex, for correctly pointing out the issue.

4
  • What should the code do if the length is not any of 10, 15, 20, or 25? If the answer is "that shouldn't happen, so the code should crash", great! That's what it's doing already :-) Commented Apr 19, 2018 at 19:53
  • 4
    It means it went through all the ifs and none of them were true. Commented Apr 19, 2018 at 19:54
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Commented Apr 19, 2018 at 19:54
  • Also you really don't need any if statements or even an intermediate variable in this case. Just go for index, row in df.head(listLength).iterrows(): Commented Apr 19, 2018 at 19:57

3 Answers 3

2

You need to make sure the listLength variable has a value. In the example below I'm initializing it with the empty list [], so that when none of the conditions match, it is still defined.

listLength = []
if resultsLength == 10:
    listLength = df.head(10).iterrows()
elif resultsLength == 15:
    listLength = df.head(15).iterrows()
elif resultsLength == 20:
    listLength = df.head(20).iterrows()
elif resultsLength == 25:
    listLength = df.head(25).iterrows()
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution but putting the default into an else clause at the end is frequently a good choice, especially if that is in fact an error condition and something should be logged or raised.
1

Python is not running the code asynchronously, all your conditions such as resultsLength == 10 were false because resultsLength is something else.

In this case your if statements are not needed and you should just write:

for index, row in df.head(listLength).iterrows():

In the more general case where the if statements contain something more complicated and you really only want to allow a few values, I would recommend adding something like this at the end of your elif chain:

else:
    raise ValueError('Unhandled case listLength = %r' % listLength)

This will immediately alert you of the problem and let you know what listLength is.

Comments

0

You ran that block but resultsLength was neither 10, nor 15, nor 20, nor 25, hence none of the if/elif block got executed and listLength was never set.

You get the error https://docs.python.org/3/library/exceptions.html#UnboundLocalError

To solve your problem, you can make sure resultsLength is set to an empty list before your first if, or you can use else to set it to an empty list as in:

resultsLength = []
if ...

or

if ...
elif ...
else:
    resultsLength = []

1 Comment

Too late! This answer exists. Sometimes its a question of sheer typing speed.

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.