繁体   English   中英

Python脚本TypeError:“ int”对象不可调用

[英]Python Script TypeError: 'int' object is not callable

我编写了一个供个人使用的脚本来解决物理方程式。 我是python noob,这是我尝试使用自己的技能并进一步了解该语言的尝试。 但是,当我不为变量输入值时,我的脚本将直接针对该变量进行求解。 这适用于除我的'accel'变量以外的所有变量,该变量会在标题中吐出给定的错误。 脚本中断并在以下行给出错误:

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

任何帮助,将不胜感激。 我导入的二次公式脚本不会影响此错误,因为当我注释掉导入时,我遇到了同样的错误,请告诉我是否应该发布该代码。 是的,我浏览了类似的问题,没有,它们没有帮助我。

    __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()

问题是这一行:

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

应该是这样的:

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

本质上,通过执行2(...) ,您是在要求Python查找并调用名为2的函数。 但是, 2是数字,不是函数! 自然,事情破裂了。

请注意,错误消息为您提供了有关错误原因的提示。 该错误信息是, int is not callable -这意味着你的东西处理作为本来就不是一个函数的函数。 从那里开始,很容易地扫描所讨论的行以查找可能出了问题的地方。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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