简体   繁体   English

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

[英]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. 我是python noob,这是我尝试使用自己的技能并进一步了解该语言的尝试。 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. 这适用于除我的'accel'变量以外的所有变量,该变量会在标题中吐出给定的错误。 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 . 本质上,通过执行2(...) ,您是在要求Python查找并调用名为2的函数。 However, 2 is a number, not a function! 但是, 2是数字,不是函数! 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. 该错误信息是, int is not callable -这意味着你的东西处理作为本来就不是一个函数的函数。 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))

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

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