我一直在学习物理学的python,
即时通讯使用VIDLE - Py 2.7,
我打开一个新文件而不保存输入此代码:
scene.width = 800
scene.height = 600
scene.autoscale = 0
scene.range = (100, 100, 100)
scene.center = (0, 40, 0)
#scene.fullscreen = 1
ball = sphere(pos=(0,103,1),radius = 2)
ground = box(pos=(0,-1,0),size=(100,2,100))
building = box(size = (6,100,6),pos=(0,50,0),color=color.blue)
gravity = 9.8 # m/s**2
velocityX = 7 #m/s
seconds = 0
dt = 0.05
finished = False
while not finished:
rate(100) # dont run through loop more than 100 times/sec
seconds += dt
#position equation; y(t) = y0 + v0*t + .5 * a * t**2
ballY = 100 - .5 * gravity * seconds**2
ballX = velocityX * seconds
ball.pos = vector(ballX, ballY, 0)
if ballY -2 <=0:
finished = True
print "seconds to drop: " + str(seconds)
这成功运行程序,但当我将其保存为.py然后尝试以相同的方式再次运行它时出现错误
追踪(最近一次通话): 文件“/Users/bencallaghan/Desktop/psyre.py”,第1行 从视觉导入* 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/VPython-6.05-py2.7-macosx-10.6-intel.egg/visual/init .py“,第34行 来自visual_common.create_display import * 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/VPython-6.05-py2.7-macosx-10.6-intel.egg/visual_common/create_display.py”,第10行 将wx导入为_wx 文件“/usr/local/lib/wxPython-2.9.4.0/lib/python2.7/site-packages/wx-2.9.4-osx_cocoa/wx/init.py”,第45行 来自wx._core import * 文件“/usr/local/lib/wxPython-2.9.4.0/lib/python2.7/site-packages/wx-2.9.4-osx_cocoa/wx/_core.py”,第5行 导入新的 文件“/Users/bencallaghan/Desktop/new.py”,第8行 来自pylab import scatter,xlabel,ylabel,xlim,ylim,show ImportError:没有名为pylab的模块
我的猜测是它在Vpython中的某种类型的shell中运行,可以访问visual和pylab,但是当我保存它然后尝试从其他地方访问它们时它不能。 但除了这个推理,我不知道从那里去哪里 任何想法?
答案 0 :(得分:0)
对于初学者,您需要以与在第一行导入vpython相同的方式导入pylab。
from pylab import*
此外,您似乎需要以这种方式缩进,以便您的位置/速度更新包含在您的while循环中。希望这有帮助
while not finished:
rate(100) # dont run through loop more than 100 times/sec
seconds += dt
#position equation; y(t) = y0 + v0*t + .5 * a * t**2
ballY = 100 - .5 * gravity * seconds**2
ballX = velocityX * seconds
ball.pos = vector(ballX, ballY, 0)
if ballY -2 <=0:
finished = True
print "seconds to drop: " + str(seconds)
PS不确定&#34; import&#34;之间的空格。和&#34; *&#34;在你的第一行引起问题,应该是:
from visual import*
不
from visual import *
但这可能没问题