繁体   English   中英

AttributeError:“ str”对象没有属性“ getSA_H”

[英]AttributeError: 'str' object has no attribute 'getSA_H'

我收到此错误。 我已经尝试了一切-如果有人可以帮助我,那将是很棒的。

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'

我试图重现您的代码,将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适用于当前的班级属性。

您可以在此处找到有关使用对象或访问文档中的类属性的更多信息。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM