Python脚本类型错误:';int';对象不可调用
我做了一个脚本供个人使用,用来解一个物理方程。我是PythonNoob,这是我尝试使用我的技能并学习更多关于该语言的知识。然而,当我没有为一个变量输入值时,我的脚本将被引导为求解该变量。这适用于除我的“accel”变量之外的所有变量,它会在标题中指出给定的错误。脚本中断,并在以下行出现错误:Python脚本类型错误:';int';对象不可调用,python,typeerror,Python,Typeerror,我做了一个脚本供个人使用,用来解一个物理方程。我是PythonNoob,这是我尝试使用我的技能并学习更多关于该语言的知识。然而,当我没有为一个变量输入值时,我的脚本将被引导为求解该变量。这适用于除我的“accel”变量之外的所有变量,它会在标题中指出给定的错误。脚本中断,并在以下行出现错误: accel = ((2(x - x0 - (v0 * t))) / (t ** 2)) 任何帮助都将不胜感激。我导入的二次公式脚本不应该影响这个错误,因为当我注释掉导入时,我得到了相同的错误,请告诉我是否
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是不可调用的
——这意味着您将某个东西视为一个函数,而实际上它不是一个函数。从那里,可以很容易地扫描相关行,找出可能出现的错误。问题是这行:
accel = ((2(x - x0 - (v0 * t))) / (t ** 2))
应该是这样的:
accel = ((2 * (x - x0 - (v0 * t))) / (t ** 2))
本质上,通过执行2(…)
,您要求Python查找并调用名为2
的函数。但是,2
是一个数字,而不是一个函数!很自然,事情发生了变化
请注意,错误消息给了您一个错误提示。错误消息是,
int是不可调用的
——这意味着您将某个东西视为一个函数,而实际上它不是一个函数。从那里,我们可以很容易地扫描所讨论的行,找出可能出现的错误。accel=((2(x-x0-(v0*t))/(t**2))
应该是accel=((2*(x-x0-(v0*t))/(t**2))
accel=((2(x-x0-(v0*t))/(t**2))
应该是accel=((2*(x-x0-(v0*t))/(t**2))
是。2(任何东西)
看起来您将数字2
视为一个函数(因此错误消息中的“可调用”)。没有调用的函数2()
…非常感谢您的出色工作。我每天都在学习越来越多关于Python的知识。也感谢您Floris提供的额外信息。是的。2(任何)
看起来您将数字2
视为一个函数(因此错误消息中有“callable”(可调用))。没有名为2()
…非常感谢您的工作。我每天都在学习越来越多关于Python的知识。也感谢Floris提供更多信息。