简体   繁体   中英

Python class problem "AttributeError: 'int' object has no attribute isSolvable"

I'm try to learning class properties in Python. I'm trying to solve a 2X2 equation. After getting 6 values ​​from the user, I try to make a transaction in the class but it gives an error. Can you help me?

import numpy as np

class HomogenEquation():

     def __init__(self,number): 
        self.number = number
        self.value=0  

     def isSolvable(self,a,d,b,c):
         return (a*d)-(b*c)==0

     def getXY(self,a,b,c,d,e,f):
         x = np.array([[a, b],[c,d]])
         y = np.array([e, f])
         print(np.linalg.solve(x,y))


a=int(input("a: "))
a_value= HomogenEquation(a)

b=int(input("b: "))
b_value= HomogenEquation(b)

e=int(input("e: "))
e_value= HomogenEquation(e)

c=int(input("c: "))
c_value= HomogenEquation(c)

d=int(input("d: "))
d_value= HomogenEquation(d)

f=int(input("f: "))
f_value= HomogenEquation(f)

if a.isSolvable(a,d,b,c):

    getXY(a,b,c,d,e,f)
else:
    print("The equation has no solution.")

First of all, if I understood the purpose of your class, you're implementing it incorrectly.


Here are my thoughts:

You don't need to create multiple instances of this class for each number, because your class is not a number class. It's an equation class. Assuming that, your code needs to be restructured. There are multiple implementations that can take place, but your's make no sense to me.
Things you can improve:

  • Pass numbers all together to your equation class
  • Not creating multiple instances of equations for each number (makes no sense)

So, minimally changing your code getting the result:

import numpy as np


class HomogenEquation():
     def __init__(self, a, b, c, d, e, f): 
        # initializing class with all variables all together
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e
        self.f = f

     def isSolvable(self):
         return (self.a * self.d) - (self.b * self.c) == 0

     def getXY(self):
         x = np.array([[self.a, self.b],[self.c, self.d]])
         y = np.array([self.e, self.f])
         return np.linalg.solve(x, y)

Allright, so now class structure makes sense (but there are some things to improve still)


So, to use it you just need to initialize an instance with all numbers and call t's methods:

# btw this input looks not right, but I'll leave it

a=int(input("a: "))
b=int(input("b: "))
e=int(input("e: "))
c=int(input("c: "))
d=int(input("d: "))
f=int(input("f: "))

equation = HomogenEquation(a,b,c,d,e,f) # creating one instance for equation

if equation.isSolvable():
    print(equation.getXY())             # and calling it's methods
else:
    print("The equation has no solution.")

Comment: This code still has multiple downsides, but class usage now make sense.
I hope this will help you in your future learning!

You're referencing the wrong object. a, b, c, d, e, f are of type int where a_value, b_value, c_value, d_value, e_value, f_value are of type HomogenEquation .

You probably want:

if a_value.isSolvable(a,d,b,c):
    getXY(a,b,c,d,e,f)
else:
    print("The equation has no solution.")

You have the following class:

class HomogenEquation:

 def __init__(self,number): 
    self.number = number
    self.value=0  

 def isSolvable(self,a,d,b,c):
     return (a*d)-(b*c)==0

 def getXY(self,a,b,c,d,e,f):
     x = np.array([[a, b],[c,d]])
     y = np.array([e, f])
     print(np.linalg.solve(x,y))

If you want to collect user input and then create an object, you can do this:

# in the main method
a = int(input("Number: "))

# Collect all the other values the same way.

# Create an object of HomogenEquation
a_value = HomogenEquation(a)
print(a_value.isSolvable(a,b,c,d,e,f))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM