Fayaz Deen
Fayaz Deen

Reputation: 3

How to call int() function from another function in Python?

Following is my code:

   class MyClass:
        "This is my second class"
        a=["1224","2334","3445"]
        b=int(input("Enter the PIN number"))
        entries={1224:"X",2334:"Y",3445:"Z"}
        def check(self,int):
                    che=False
                    for c in range(0,3):
                            if (m.b==int(m.a[c])):
                                che=True
                                break
                            else:
                                che=False
                    print(che)               
        def func(int):
                    print('Hello',entries[b])
    m=MyClass()
    m.check(m.b)

Whenever I try to convert String to integer from inside a method I get the TypeError:

Enter the PIN number1224
   Traceback (most recent call last):
  File "MyClass.py", line 18, in <module>
          m.check(m.b)
  File "MyClass.py", line 9, in check
         if (m.b==int(m.a[c])):
TypeError: 'int' object is not callable

Upvotes: 0

Views: 233

Answers (1)

Professor_Joykill
Professor_Joykill

Reputation: 989

Couple things.

  1. I was unable to reproduce your error exactly however I didn't get the correct output, see part 2 for how to get proper output
  2. Why do you want to convert there input into an integer, seeing as currently your list has strings so you want to keep your input as a string.
  3. Just generally in your check method make it self.a and self.b as that means that the class can be named whatever, whereas currently it can cause issues if your class isn't named m

Edit: Also in your for loop, in the first if statement add a break after setting che to true or else it will keep running the for loop and might set che back to false after making it true

Upvotes: 1

Related Questions