AttributeError:'str'对象没有属性'getSA\u H'

2024-06-26 17:59:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个错误。我什么都试过了-如果有人能帮我就太好了。你知道吗

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()

希望有人能帮忙。我和我的朋友都不明白为什么它不起作用。你知道吗

错误消息是:

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'

Tags: inselfinputdeffloatnoteelifim
1条回答
网友
1楼 · 发布于 2024-06-26 17:59:51

我试图重现您的代码,将object传递给类可能会有所不同,因此:

class getdata_hauptfach:
 ...

class Halbjahre:
 ...

尝试:

class getdata_hauptfach(object):
 ...

class Halbjahre(object):
 ...

这样您就可以访问传递给该类的所有变量和方法。您可能还需要更改以下行:

getdata_hauptfach.getData_H(self.Halbjahr)

getdata_hauptfach().getData_H(self.Halbjahr)

因此类被初始化,self应用于当前类属性。你知道吗

您可以在docs中找到有关使用对象here或访问类属性的更多信息

下面是这种情况的一个简单例子:

## 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

相关问题 更多 >