my code give my this errore after call it takes 1 positional argument but 3 were given

huangapple go评论69阅读模式
英文:

my code give my this errore after call it takes 1 positional argument but 3 were given

问题

Here's the translated code part:

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)

The code you provided seems to be a Python class for converting Gregorian dates to Hijri dates. If you're encountering an error, it's because you need to create an instance of the class before calling the gregorian_to_hijri method. Here's how you can use it correctly:

from class_converter import Samadateconverter

# Create an instance of the Samadateconverter class
converter = Samadateconverter(1233, 5, 7)

# Call the gregorian_to_hijri method on the instance
result = converter.gregorian_to_hijri()

# Now 'result' will contain the Hijri date

This should resolve the error you mentioned.

英文:
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)

and then in another file i call it

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

答案1

得分: 1

你需要在调用方法之前创建你的类的实例:

conv = Samadateconverter(1233, 5, 7)  # 这会调用构造函数 __init__
year, month, day = conv.gregorian_to_hijri()  # 这会调用你的方法

输出:

>>> year, month, day
(7, 9, 220376)
英文:

You have to create an instance of your class before then call your method:

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

Output:

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

答案2

得分: 1

Your function is defined as only taking one parameter: self

self is automatically passed in when gregorian_to_hijri() is called on an instance of your Samadateconverter object. When doing that, giving the function any arguments at all will result in this error.

If I am reading this right, you want your Samadateconverter object to hold values:

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

You should change your code to be:

from class_converter import Samadateconverter

converter = Samadateconverter(1233, 5, 7)
year, month, day = converter.gregorian_to_hijri()
英文:

Your function is defined as only taking one parameter: self

self is automatically passed in when gregorian_to_hijri() is called on an instance of your Samadateconverter object. When doing that, giving the function any arguments at all will result in this error.
If I am reading this right, you want your Samadateconverter object to hold values:

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

You should change your code to be:

from class_converter import Samadateconverter

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

答案3

得分: 1

这将起作用

from class_converter import Samadateconverter

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

this will work

from class_converter import Samadateconverter

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

huangapple
  • 本文由 发表于 2023年4月4日 04:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定