'I cant call a class method in Python; AttributeError: 'check_class' object has no attribute 'check_1'

I want to check if the games have numbers in a particular key?

But i cant call the the class method check_1(), can you help me out? It gives me 'check_class' object has no attribute 'check_1'.

And do you have a idea how i can avoid the global variabel and the global fuction for the recursion, because its bad practice?

I am thankful for every suggestion to optimize the code, i am relatively new to programing.

from queue import Empty

class check_class :
    
    def __init__(self, dict1={}, dict2={}, dict3={}, dict4={}, dict5={}, dict6={}, dict7={}, dict8={}, dict9={}, dict10={}, dict11={}, dict12={}, dictionary_of_games ={}):
        self.dict1 = dict1
        self.dict2 = dict2
        self.dict3 = dict3
        self.dict4 = dict4
        self.dict5 = dict5
        self.dict6 = dict6
        self.dict7 = dict7
        self.dict8 = dict8
        self.dict9 = dict9
        self.dict10 = dict10
        self.dict11 = dict11
        self.dict12 = dict12
        self.dictionary_of_games = dictionary_of_games

        dictionary_of_games = [
        self.dict1,
        self.dict2,
        self.dict3,
        self.dict4,
        self.dict5,
        self.dict6,
        self.dict7,
        self.dict8,
        self.dict9,
        self.dict10,
        self.dict11,
        self.dict12
        ]

    global played_games_of_the_day
    played_games_of_the_day =[]

    global check_1
    def check_1(self, **kwargs):
        hht = kwargs.get("halbzeit_h_tore")
        hat = kwargs.get("halbzeit_a_tore")
        c = 1
        if hht == "-" and hat == "-":
            played_games_of_the_day.append(0)
            c += 1
            return check_1(self, self.dictionary_of_games[c])
        elif int(hht) == int and int(hat) == int :
            c += 1
            played_games_of_the_day.append(1)
            return check_1(self, self.dictionary_of_games[c])
        #if dict empty pass#
        elif Empty:
            pass
        
        for i in played_games_of_the_day:
            if all(x==0 for x in played_games_of_the_day):
                print("all zero")
            elif all(x==1 for x in played_games_of_the_day):
                print("all one")

a = {0: {'spieltag': '1. Spieltag:', 'tag': 'Fr', 'd_u': '13.08.2021 20:30', 'team1': 'Borussia M´gladbach', 'team2': 'Bayern München', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     1: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'SC Freiburg', 'h_tore': '0', 'a_tore': '0', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     2: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfL Wolfsburg', 'team2': 'VfL Bochum', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     3: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'TSG Hoffenheim', 'h_tore': '0', 'a_tore': '4', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     4: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'Greuther Fürth', 'h_tore': '5', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     5: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     6: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 18:30', 'team1': 'Borussia Dortmund', 'team2': 'Eintracht Frankfurt', 'h_tore': '5', 'a_tore': '2', 'halbzeit_h_tore': '3', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     7: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'RB Leipzig', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     8: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 17:30', 'team1': '1. FC Köln', 'team2': 'Hertha BSC Berlin', 'h_tore': '3', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}}

b = check_class(a)
b.check_1(b.dictionary_of_games[0])

I'm getting an AttributeError: 'check_class' object has no attribute 'check_1'



Solution 1:[1]

Yes, your example code has several problems. You will have to examine the concept of scope a little more. Note the use of the variable self.played_games_of_the_day. Also the call to the constructor did not set the dictionary_of_games - it was always set to an empty dict.

I have corrected your code so that it might theoretically run, but I am not sure what is supposed to do. In so doing I have removed the dicts dict1, dict2, etc. as they were not being used.

In future you should post the errors that python gives you to be more precise about your problem and reduce your problem to a minimal example...

from queue import Empty

class Check_class :
    
    def __init__(self, dictionary_of_games):
        self.dictionary_of_games = dictionary_of_games
        self.played_games_of_the_day = []

    def check_1(self):
        for entry in self.dictionary_of_games.values():
            hht = entry.get("halbzeit_h_tore")
            hat = entry.get("halbzeit_a_tore")
            if hht == "-" and hat == "-":
                print(f"{hht} {hat}")
                self.played_games_of_the_day.append(0)
            elif hht.isnumeric() and hat.isnumeric():
                print(f"{hht}:{hat}")
                self.played_games_of_the_day.append(1)
            #if dict empty pass#
            elif Empty:
                pass
        
        if all(x==0 for x in self.played_games_of_the_day):
            print("all zero")
        elif all(x==1 for x in self.played_games_of_the_day):
            print("all one")

a = { 0: {'spieltag': '1. Spieltag:', 'tag': 'Fr', 'd_u': '13.08.2021 20:30', 'team1': 'Borussia M´gladbach', 'team2': 'Bayern München', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    1: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'SC Freiburg', 'h_tore': '0', 'a_tore': '0', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    2: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfL Wolfsburg', 'team2': 'VfL Bochum', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '},
    3: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'TSG Hoffenheim', 'h_tore': '0', 'a_tore': '4', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    4: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'Greuther Fürth', 'h_tore': '5', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    5: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    6: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 18:30', 'team1': 'Borussia Dortmund', 'team2': 'Eintracht Frankfurt', 'h_tore': '5', 'a_tore': '2', 'halbzeit_h_tore': '3', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    7: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'RB Leipzig', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    8: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 17:30', 'team1': '1. FC Köln', 'team2': 'Hertha BSC Berlin', 'h_tore': '3', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}
    }
    
b = Check_class(a)
b.check_1()
print('Done!')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1