python 我的代码给予我这个错误后,调用它需要1个位置参数,但3被给予

ws51t4hk  于 2023-04-04  发布在  Python
关注(0)|答案(3)|浏览(214)
class Samadateconverter:
    def __init__(self, y , m , d ):
        self.y = y
        self.m = m
        self.d = d
                 
    def gregorian_to_hijri(self):
        # Calculate the Julian Day Number (JDN) for the Gregorian date
        jdn = int((1461 * (self.y + 4800 + int((self.m - 14) / 12))) / 4) + int((367 * (self.m - 2 - 12 * int((self.m - 14) / 12))) / 12) - int((3 * int((self.y + 4900 + int((self.m - 14) / 12)) / 100)) / 4) + self.d - 32075

        # Calculate the Hijri calendar date from the JDN
        h = int((jdn - 1948440 + 10632) / 10631)
        year = int((h + 10307) / 1335)
        month = ((h - 1) % 12) + 1
        day = jdn - 1948440 - int((year * 354) + int((3 + (11 * year)) / 30)) - int((month - 1) * 29.5) + 1

        return (year, month, day)

然后在另一个文件中我称之为

from class_converter import Samadateconverter

Samadateconverter.gregorian_to_hijri(1233,5,7)

and its gave my this errore gregorian_to_hijri()takes 1 positional argument but 3 were given

of1yzvn4

of1yzvn41#

你必须先创建一个类的示例,然后才能调用你的方法:

conv = Samadateconverter(1233,5,7)  # This calls the constructor __init__
year, month, day = conv.gregorian_to_hijri()  # This calls your method

输出:

>>> year, month, day
(7, 9, 220376)
ibps3vxo

ibps3vxo2#

你的函数被定义为只接受一个参数:self
Samadateconverter对象的示例上调用gregorian_to_hijri()时,self会自动传入。当这样做时,给函数任何参数都会导致这个错误。如果我没有阅读错,你希望你的Samadateconverter对象保存值:

self.y = 1233
self.m = 5
self.d = 7

您应该将代码更改为:

from class_converter import Samadateconverter

converter = Samadateconverter(1233,5,7)
year, month, day = converter.gregorian_to_hijri()
olmpazwi

olmpazwi3#

这会起作用的

from class_converter import Samadateconverter

s = Samadateconverter(1233,5,7)
year, month, day = s.gregorian_to_hijri()

相关问题