简体   繁体   中英

Python error “'int' object is not callable”

I wrote some code for a program that gives me the values of a sequence I defined as a function, f(x) , but when I run it a error pops saying 'int' object is not callable ". Does anyone know how to solve the problem?

def f(x):
    if x%2==0:
        return x/2
    else:
        return 3*x+1
limite=int(input("parar en: "))   
x=int(input("a1: "))
print(x)
n=1
while n<=limite:
    f=f(x)
    print(f)
    n=n+1
print("fin")

You're reassigning f within the while loop, to the result of calling f(x) , which indeed an integer. So, the second time through the loop, f is an integer rather than a function.

I suspect you simply meant:

x = f(x)
print x

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