Find answers, ask questions, and connect with our <br>community around the world.

Home Forums Scientific computing with Python assignment on python

  • assignment on python

    Posted by Premkumar Murugesan on February 21, 2024 at 6:07 pm

    Hi I am trying to find factorial for a number using class methods, but am not getting answer

    can anyone explain what is the problem in my code

    PROGRAM:

    class mathutils():

    def __init__(a,x):

    a.fac=x

    def fact(a,y):

    b=1

    while (a.fac>1):

    b=b*(a.fac)

    (a.fac)=(a.fac)-1

    return b

    c=int(input(“enter the number “))

    e=mathutils(c)

    print(e)

    OUTPUT:

    enter the number 5
    <__main__.mathutils object at 0x000001C750328710>
    Rahul replied 3 months, 3 weeks ago 2 Members · 2 Replies
  • 2 Replies
  • Premkumar Murugesan

    Member
    February 21, 2024 at 6:09 pm
  • Rahul

    Member
    February 23, 2024 at 6:37 am

    Hi Premkumar.
    Though you have created a constructor for the mathutils function, you have not created an object for the class, using the input ‘c’. This was the reason for the error.
    Also, mathutils is a library in Python and it is recommended to not use similar names as it will create issues in debugging and understandability of the code when you work on larger projects. Another convention is to use the variable name as ‘self’ instead of ‘a’ to reference variables and functions of the same class. It is not a keyword, but a convention which may be useful when writing larger or more complex code. Please see the modified code attached.

    Best regards
    Rahul
    Flowthermolab

Log in to reply.

error: Content is protected !!