A
A
ace6062021-09-06 13:25:10
Python
ace606, 2021-09-06 13:25:10

NameError: name 'symbol' is not defined. Where did you go wrong in the code?

Good day. I can not understand why the interpreter produces an error. Help!
Function in a module.

Traceback (most recent call last):
  File "C:\Users\Ace\Desktop\работа\# Подсчет распространненых чисел POWERBALL.py", line 22, in <module>
    main()
  File "C:\Users\Ace\Desktop\работа\# Подсчет распространненых чисел POWERBALL.py", line 16, in main
    f_find_simbol_ver2.f_find_max(result)
  File "C:\Users\Ace\Desktop\работа\f_find_simbol_ver2.py", line 53, in f_find_max
    return symbol.append(n)
NameError: name 'symbol' is not defined

import f_find_simbol_ver2
symbol = []
frequency = []
def main():
    global symbol 
    global frequency 
    infile = open('POWERBALL2.txt', 'r')
    line = infile.readline()
    result = ''
    while line != '':
        result += line.rstrip('\n')
        result += ' '
        line = infile.readline()
    infile.close()    
    f_find_simbol_ver2.f_find_max(result)
main()

def f_find_max(x):
    global symbol
    global frequency
    x = x.split(' ')
    n = x[0]
    stop = False
    total = 0
    for ch in x:
        if n == ch:
            total += 1
    result = []
    result2 = []
    length = len(x)
    total2 = 0
    no_search = []
    no_search.append(n)
    for index in range(1, length, 1):
        for i in no_search:
            if str(i) == str(x[index]):
                stop = True
        if stop == False:
            for c in x:
                if str(x[index]) == str(c):
                    total2 += 1
            if total < total2:
                n = x[index]
                total = total2
                total2 = 0
                result = []
                result2 = []
                no_search.append(x[index])
            elif total == total2:
                result.append(x[index])
                result2.append(total2)
                no_search.append(x[index])
                total2 = 0
            else:
                total2 = 0
                no_search.append(x[index])
        stop = False
    if result != []:
        result.append(n)
        result2.append(total)
        for e in result:
            symbol.append(e)
        for e2 in result2:
            frequency.append(e2)
    else:
        print('Символ, который появляется наиболее часто', n, sep = ' ')
        print(n) 
        print(total)
        symbol.append(n)
        frequency.append(total)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-06
@ace606

Different modules have different global variables.
In general, global is better not to use unnecessarily.
Accept lists to be populated as method parameters. Better yet, create them inside the method and return them as a tuple: return symbol, frequency . Well, or write a simple class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question