簡體   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