gpt4 book ai didi

python - 属性错误: 'str' object has no attribute 'getSA_H'

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:45 25 4
gpt4 key购买 nike

我收到此错误。我已经尝试了一切 - 如果有人可以帮助我,那就太好了。

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

希望有人能帮忙。我和我的 friend 不明白为什么它不起作用。

错误信息是:

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 应用于当前类属性。

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

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

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

关于python - 属性错误: 'str' object has no attribute 'getSA_H' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com