“int”对象在自制类Python中没有属性“getal”

2024-06-01 00:54:38 发布

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

这个程序的思想是把一种“语言”变成数字。语言很简单

K = 10 P = 20 T = 40 V = 80 anything smaller then 10 will be represented in normal numbers

现在,数字不应该是什么担心我解释这一点,所以它更容易得到我试图实现这里。你知道吗

我建立了一个名为“Mangarevaans”的类,如下所示:

def mag2arab(getal):    #this function is designed to turn the letters into the normal numbers we're used to 
mag = str(getal)
waarde = {"K": 10, "P": 20, "T": 40, "V": 80}
arab = 0

for index, j in enumerate(mag):
    if index == 0 and j.isnumeric():

        if len(getal) == 1:
            x = 0
        else:
            x = 1

        arab += int(j) * waarde[mag[x]]
    elif j.isnumeric():
        arab += int(j)
    elif not (str(mag[0]).isnumeric() and index == 1):
        arab += waarde[j]
return arab

class Mangarevaans():
    """
    >>>612 // Mangarevaans(26)
    Mangarevaans('P3')
    """

    def __init__(self, getal):

        if isinstance(getal, int):
            assert 1 <= getal < 799, 'ongeldige waarde'   #this is one of the rules of the language that if there is a number it should be between these values
            self.getal = getal


        else:
            for letter in getal:
                if isinstance(getal, str):
                    for letter in getal:
                        if letter in "VTPK":
                            self.getal = getal
                else:
                    raise AssertionError('ongeldige waarde')
            self.getal = mag2arab(getal)

    def __int__(self):
        return self.getal

    def __str__(self):
        return arab2mag(self.getal)

    def __repr__(self):
        return f"Mangarevaans('{str(arab2mag(self.getal))}')"

    def __rfloordiv__(other, self):
        return Mangarevaans(other // self.getal) #The problem occurs here 

现在当我想做医生测试的时候

    """
    >>>612 // Mangarevaans(26)
    Mangarevaans('P3')
    """

我听到一个错误说

'int' object has no attribute 'getal'

但如果我把自己变成一根弦

'str' object has no attribute 'getal'

如何定义属性“getal”是属于“str”还是“int”?你知道吗

有人能帮我吗?你知道吗

已经非常感谢了


Tags: theinselfforreturnifisdef