Los Drakos
Los Drakos

Reputation: 13

Can't pass variable to function

can't understand what is wrong with this code.

I keep getting this error:

  File "c:\Users\jachy\Desktop\Coding\Nová složka\tipsntricks.py", line 28, in login_clicked
    msg = "The number is " + str(check_for_prime(number.get))
  File "c:\Users\jachy\Desktop\Coding\Nová složka\tipsntricks.py", line 16, in check_for_prime
    for i in range(2, int(x) + 2):
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'method'
def check_for_prime(x):
    for i in range(2, int(x) + 2):
        if int(x) % i == 0 and int(x) != i:
            return "not prime number"
            break
        if i > int(x):
            return "prime number"
            break
        

def login_clicked():


    msg = "The number is " + str(check_for_prime(int(number.get)))
    showinfo(title='Information', message=msg)

I am uding tkinter and i want to tell me, if the number is prime or not. I really don't understand why x is not variable, but method.

thanks for advices.

Upvotes: 1

Views: 114

Answers (1)

Eli Harold
Eli Harold

Reputation: 2301

You have to use () to call number.get like so:

number.get() #correct usage

Full code:

def check_for_prime(x):
    for i in range(2, int(x) + 2):
        if int(x) % i == 0 and int(x) != i:
            return "not prime number"
            break
        if i > int(x):
            return "prime number"
            break
        
def login_clicked():
    msg = "The number is " + str(check_for_prime(int(number.get()))) #notice the change here
    showinfo(title='Information', message=msg)

Further explanation: number.get returns the method .get, but number.get() calls .get and returns what it is supposed to return.

Upvotes: 2

Related Questions