0
def opt_b_private():
    next(data_file)
    for row in data_file:
        list_data = row[1:3]
        list_lol.extend(list_data) 
    list_b =list(map(int,list_lol))
    sum_b = sum(list_b)
    length_b = len(list_b)
    average_b = sum_b / length_b
    print(f'the number average number of private dentist in 10 year span from 2010-2019 is {average_b} dentist')

this was the error when i run it a second time but run it first time it was ok.i do not know how to solve the issue for this an anyone help me ? thank you!

File "C:\Users\hongi\inputproject.py", line 24, in opt_b_private
    next(data_file)

StopIteration
sam kin
  • 11
  • 3

1 Answers1

-1

When I run

def opt_b_private():
    next(data_file)
    for row in data_file:
        list_data = row[1:3]
        list_lol.extend(list_data) 
    list_b =list(map(int,list_lol))
    sum_b = sum(list_b)
    length_b = len(list_b)
    average_b = sum_b / length_b
    print(f'the number average number of private dentist in 10 year span from 2010-2019 is {average_b} dentist')

I get the error message

NameError: name 'data_file' is not defined

The likely reason (at least for this code snippet) is that there is a variable data_file which is undefined.

The function takes no arguments, but the function depends on arguments that are not locally available in the scope.

Ben
  • 563
  • 1
  • 5
  • 12
  • 1
    That you can't reproduce the OP's problem is not a solution to that problem. (It _is_ a reason to vote to close their question as not providing a [mre]). – Charles Duffy Jun 07 '21 at 19:31
  • oh before this i have a csv reader that imports the csv – sam kin Jun 09 '21 at 23:19
  • would this work if i use the next() function before the def? i know the issue is that i’m exhausting the iterator by using the next() in the for loop – sam kin Jun 09 '21 at 23:23
  • I don't have sufficient context for what you're trying to accomplish, but one issue is that `next()` returns the next element in a list. Is `data_file` a list? The `for row in ...` expects to operate on a list. The `list_lol` is not defined in the scope of the function – Ben Jun 11 '21 at 01:35