1

I'm getting this error. I already tried everything - would be great if someone can help me.

class getdata_hauptfach:
    def __init__(self):
        self.Schulaufgabe_Hauptfach = 0.0
        self.EX1_Hauptfach = 0.0
        self.EX2_Hauptfach = 0.0
        self.Muendliche_Note_Hauptfach = 0.0
        self.Kurzarbeit_Hauptfach = 0.0

    def getSA_H(self):
        self.Schulaufgabe_Hauptfach = float(input("Schulaufgabe im Hauptfach:"))

    def getEX1_H(self):
        self.EX1_Hauptfach = float(input("Erste Ex im Hauptfach:"))

    def getEX2_H(self):
        self.EX2_Hauptfach = float(input("Zweite Ex im Hauptfach:"))

    def getM_H(self):
        self.Muendliche_Note_Hauptfach = float(input("Mündliche Note im Hauptfach:"))

    def getK_H(self):
        self.Kurzarbeit_Hauptfach = float(input("Kurzarbeit im Hauptfach:"))

    def getData_H(self):
        count_H = 0
        while count_H <= 5:
            count_H = count_H + 1
            Notenart_H = input('Welche Note möchtest du für das Hauptfach eintragen?')
            if Notenart_H == 'Schulaufgabe':
                self.getSA_H()

            elif Notenart_H == 'Erste Ex':
                self.getEX1_H()

            elif Notenart_H == 'Zweite Ex':
                self.getEX2_H()

            elif Notenart_H == 'Mündliche Note':
                self.getM_H()

            elif Notenart_H == 'Kurzarbeit':
                self.getK_H()

import _sqlite3

from GETDATA_von_Fächer import getdata_hauptfach
from GETDATA_von_Fächer import getdata_nebenfach

conn = _sqlite3.connect('Notenberechnung.sqlite')
cur = conn.cursor()


class Halbjahre:
    def __init__(self, Halbjahr):
        self.Halbjahr = Halbjahr

    def Abfrage(self):
        self.Halbjahr = input('Welches Halbjahr?')
        self.Fachart = input('Hauptfach oder Nebenfach?')
        self.Fachname = input('Welches Fach?')

    def Speichern_in_Datenbanken(self):
        if self.Halbjahr == '1':
            if self.Fachart == 'Hauptfach':
                getdata_hauptfach.getData_H(self.Halbjahr)

            elif self.Fachart == 'Nebenfach':
                getdata_nebenfach()
                cur.execute()

        elif self.Halbjahr == 2:
            if self.Fachart == 'Hauptfach':
                getdata_hauptfach()
                cur.execute()
            elif self.Fachart == 'Nebenfach':
                getdata_nebenfach()
                cur.execute()



    def Test_finish(self):
        self.Abfrage()
        self.Speichern_in_Datenbanken()

test_Halbjahre = Halbjahre(1)
print(test_Halbjahre.Test_finish())

conn.close()

Hopefully someone can help. My friends and me don`t understand why its not working.

The error message is:

Traceback (most recent call last):
      File "/Users/user/github/Jahresnote/Erste_Abfrage.py", line 59, in <module>
        print(test_Halbjahre.Test_finish())
      File "/Users/user/github/Jahresnote/Erste_Abfrage.py", line 56, in                     Test_finish
        self.Speichern_in_Datenbanken()
      File "/Users/user/github/Jahresnote/Erste_Abfrage.py", line 22, in     Speichern_in_Datenbanken
        getdata_hauptfach.getData_H(self.Halbjahr)
      File "/Users/user/github/Jahresnote/GETDATA_von_Fächer.py", line 31, in getData_H
        self.getSA_H()
    AttributeError: 'str' object has no attribute 'getSA_H'
joanis
  • 10,635
  • 14
  • 30
  • 40
Felix
  • 113
  • 2
  • 10
  • Welcome to StackOverflow! I have reformatted the error message at the end of the page to help read it. I also noticed and fixed an indentation error in `def getSA_H(self)`, where the body was not indented as it should have been. – joanis Jul 20 '19 at 05:27

1 Answers1

0

I tried to reproduce your code and it might make a difference to pass object to the classes, so instead of:

class getdata_hauptfach:
 ...

class Halbjahre:
 ...

try:

class getdata_hauptfach(object):
 ...

class Halbjahre(object):
 ...

This way you can access all variables and methods passed to that class. You might also want to change the following line:

getdata_hauptfach.getData_H(self.Halbjahr)

to

getdata_hauptfach().getData_H(self.Halbjahr)

so your class is initialized, and self applies to the current class attribute.

You can find some more information on using objects here or about accessing classes properties in the docs

One simple example of this situation below:

## original way
class test:
    def __init__(self):
        self.one_value = 1
    def get_value(self):
        print (self.one_value)

test.get_value() 
# TypeError: get_value() missing 1 required positional argument: 'self'

## correct way
class test(object):
    def __init__(self):
        self.one_value = 1
    def get_value(self):
        print (self.one_value)

test().get_value()
# 1
realr
  • 3,652
  • 6
  • 23
  • 34
  • great it worked - but instead of getdata_hauptfach().getData_H(self.Halbjahr) I changed it to getdata_hauptfach().getData_H() - big thank you really appreciate your quick answer - have a great weekend – Felix Jul 19 '19 at 17:53
  • Anytime @Felix! Would you mind accepting as the answer if it worked? – realr Jul 19 '19 at 18:00
  • No problem sorry just forgot it – Felix Jul 19 '19 at 18:45