0
首先我的道歉,如果这是一个noob问题,但我完全是新的编码。基本的Vpython代码错误 - “TypeError:一元操作数的错误类型 - :'元组''
我正在关注在Vpython中构建谐波振荡模拟器的指南,但每次我尝试运行该程序时都会出现此错误。
File "C:/Users/Nathan/Desktop/vspring", line 14 F_spring=-k*(block.pos -L) TypeError: bad operand type for unary -: 'tuple'
有人可以看看代码并告诉我我哪里出了问题。我的字面意思是遵循了这封信的指南,因为我只是一行一行地抄写它告诉我的内容。但它没有问题的拍摄。它只是指出:“你的计划是现在就可以运行”
from visual import *
from visual.graph import *
L=vector(1,0,0)
s=vector(1.,0,0)
block=sphere(radius=0.25, color=color.cyan, pos=L+s)
spring=cylinder(pos=(0,0,0), axis=L+s, radius=.1)
scene.autoscale=0
posgraph=gcurve(color=color.green)
k=100,; g=9.8; mu_s=.5; mu_k=.5; m=1.; dt=.0001; d=0.; t=0.
F_mu_s=mu_s*m*g
F_spring=-k*(block.pos-L)
almost_zero_p=mag(F_spring)*dt
block.p=vector(0,0,0)
while not(mag(block.p)<almost_zero_p and F_mu_s>=mag(F_spring)):
if not(mag(block.p)<almost_zero_p):
F_mu_k=m*g*mu_k*norm(block.p)
else:
F_ms_k=vector(0,0,0)
F_spring=-k*(block.pos-L)
Fnet=F_spring+F_mu_k
block.p=block.p+Fnet*dt
block.pos=block.pos+block.p/m*dt
spring.axis=block.pos-spring.pos
d=d+mag(block.p/m)*dt
posgraph.plot(pos=(t,block.pos.x-L.x))
t=t+dt
print "Total distance traveled is", d
啊你传说中的谢谢。所以我把一个逗号代替了一个创建元组的全称。说实话,我正在看k = 100下的线,这没有帮助大声笑。 – user2081549
没问题:)你很幸运,不会得到任何降价;)通常,语法问题会很快降低。 –