简体   繁体   中英

Python Script TypeError: 'int' object is not callable

I made a script for personal use to solve a physics equation. I am a python noob and this is my attempt to use my skills and learn more about the language. However, when I don't put a value in for a variable, my script is directed to solve for that variable. This works with every variable except my 'accel' variable, which spits out the given error in the title. The script breaks and gives an error at the following line:

accel = ((2(x - x0 - (v0 * t))) / (t ** 2))

Any help would be appreciated. The quadratic formula script that I import shouldn't be affecting this error because when I comment out the import I get the same error, let me know if I should post that code though. Yes I looked through the similar questions and no they did not help me.

    __author__ = '[PDMC] Jeteroll'

import Quadratic_Formula

def dist():

    print 'Enter the values for the following variables. Leave the variable blank for the value you wish to solve for.'

    while True:
        try:
            x = float(raw_input('Value of x (final distance): '))
        except ValueError:
            solve_for = 'x'
            print 'Will solve for x'
        break

    while True:
        try:
            x0 = float(raw_input('Value of x0 (initial distance): '))
        except ValueError:
            solve_for = 'x0'
            print 'Will solve for x0'
        break

    while True:
        try:
            v0 = float(raw_input('Value of v0 (initial velocity): '))
        except ValueError:
            solve_for = 'v0'
            print 'Will solve for v0'
        break

    while True:
        try:
            t = float(raw_input('Value of t (time): '))
        except ValueError:
            solve_for = 't'
            print 'Will solve for t'
        break

    while True:
        try:
            accel = float(raw_input('Value of a (acceleration): '))
        except ValueError:
            solve_for = 'accel'
            print 'Will solve for a'
        break

    if solve_for == 'x':
        x = (x0 + ( v0 * t) + (.5 * accel * (t ** 2)))
        print 'The value of x is: %s' % (round(x,2))

    elif solve_for == 'x0':
        x0 = (-((v0 * t) + (.5 * accel * (t ** 2)) - x))
        print 'The value of x0 is: %s' % (round(x0,2))

    elif solve_for == 'v0':
        v0 = ((x - x0 - (.5 * accel * (t ** 2))) / t)
        print 'The value of v0 is: %s' % (round(v0, 2))

    elif solve_for == 't':
        quad_a = (.5 * accel)
        quad_b = (v0)
        quad_c = (x0 - x)
        t1, t2 = Quadratic_Formula.main(quad_a, quad_b, quad_c)
        print 'The values of t are: %s, and %s' % (t1, t2)

    elif solve_for == 'accel':
        accel = ((2(x - x0 - (v0 * t))) / (t ** 2))
        print 'The value of v0 is: %s' % (round(accel, 2))

    else:
        print 'You did not specify a variable to solve for'

dist()

if __name__ == '__main__':
    dist()

The problem is this line:

accel = ((2(x - x0 - (v0 * t))) / (t ** 2))

It should be this:

accel = ((2 * (x - x0 - (v0 * t))) / (t ** 2))

Essentially, by doing 2(...) , you were asking Python to look for and call a function named 2 . However, 2 is a number, not a function! Naturally, things broke.

Notice that the error message gave you a hint as to what was wrong. The error message was that int is not callable -- that meant that you were treating something as a function which wasn't actually a function. From there, it was fairly easy to scan the lines under question to find what might have gone wrong.

accel = ((2(x - x0 - (v0 * t))) / (t ** 2))应该是accel = ((2 * (x - x0 - (v0 * t))) / (t ** 2))

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