Function 如何在vPython中调用不同函数中的函数

Function 如何在vPython中调用不同函数中的函数,function,while-loop,call,vpython,Function,While Loop,Call,Vpython,更新:忽略首要问题。我弄明白了(多亏了吉尔奇)。我现在唯一需要知道的是下面的第二个问题…如何在另一个函数中调用单独的函数 当while语句完成并且我重置时,我不能再移动球,因为while循环不会重新启动,所以我不能重新升起球以再次下落。我必须完全重新启动代码。我是vpython的初学者(在物理课上这么做),我找不到重新启动while循环的方法(或者其他方法来修复它) 我很确定这是因为while循环结束,然后当球的位置和速度重置时,while循环不会重新启动,所以我不能移动球,但我不确定如何做。谢

更新:忽略首要问题。我弄明白了(多亏了吉尔奇)。我现在唯一需要知道的是下面的第二个问题…如何在另一个函数中调用单独的函数

当while语句完成并且我重置时,我不能再移动球,因为while循环不会重新启动,所以我不能重新升起球以再次下落。我必须完全重新启动代码。我是vpython的初学者(在物理课上这么做),我找不到重新启动while循环的方法(或者其他方法来修复它)

我很确定这是因为while循环结束,然后当球的位置和速度重置时,while循环不会重新启动,所以我不能移动球,但我不确定如何做。谢谢所有能帮忙的人

代码:

GlowScript 2.7 VPython #不跑就开始 运行=错误 #启动“运行”时 def运行(b): #从外部引入运行变量 全球运行 #按下运行/暂停按钮时,在运行和暂停程序之间切换 运行=不运行 #按下运行按钮时,将文本更改为“暂停” 如果正在运行:bbutton.text=“暂停” #按下暂停按钮时,将文本更改为“运行” 其他:bbutton.text=“运行” #在程序上方创建运行按钮 bbutton=按钮(text=“Run”,pos=scene.title\u锚定,bind=Run) #当“重置”启动时 def重置(c): #从外部引入t和ball变量 球 #设置回时间=0(重置时) t=0 #将球的位置重置为初始位置(重置时) ball.pos=vec(0,2,0) #将球的速度重置为初始速度(重置时) ball.vel=vec(0,0,0) #在程序上方创建重置按钮 cbutton=按钮(text=“Reset”,pos=scene.title\u锚定,bind=Reset) #创造条件 接地=接线盒(位置=矢量(0,0,0),尺寸=矢量(10,0.2,10)) #创造球 球=球体(位置=向量(0,2,0),半径=0.4,颜色=颜色。黄色) #初速度 ball.vel=vec(0,0,0) #加速度(自由下落) g=-9.8 #初始时间 t=0 #每帧时间增加 deltat=0.01 #将“拖动”条件变为false以开始 拖动=错误 #初始鼠标位置 R=vec(0,0,0) #当你点击 scene.bind(“mousedown”,def(): #从外部引入阻力变量 全局阻力 #在鼠标单击时将“拖动”设置为true 拖动=真 #当您释放时,单击 scene.bind(“mouseup”,def(): #从外部引入阻力变量 全局阻力 #释放“单击”时将“拖动”设置为false 拖动=错误 ) ) #当为真时(程序正在运行),球略高于地面以下 #如果第二个条件不存在,球将永远不断地穿过地面 #第二种情况是,一旦球开始从平台下方移动,它就会停止,因此看起来很正常 当为真时,球位置y>地面位置y+0.48: #减速程序 费率(100) #当拖动条件为真时(当鼠标单击球时)。。。 如果拖动: #将R设置为鼠标的位置 R=scene.mouse.pos #将球位置设置为R(鼠标位置)--允许拖动球(更改球位置) ball.pos=R #程序运行时(按下运行按钮)。。。 如果正在运行: #更新球速度 #v=v0+a(时间变化) ball.vel.y=ball.vel.y+g*deltat #更新位置速度 #x=x0+v*(时间变化) ball.pos=ball.pos+ball.vel*deltat #当球碰到地面时。。。
如果(ball.pos.y尝试将while循环放入函数def中,并通过调用函数来启动它。然后您可以再次调用该函数以重新启动循环。

转到vpython.org,单击指向vpython论坛或glowscript论坛的链接,并在那里询问您的问题。

尝试制作一个mvce,您会得到更好的答案。感谢您的支持让我知道这一点。我在问题的第二部分记住了这一点。我通常会让Python初学者阅读Python的一个字节,这是一本初学者级别的免费在线书籍。我做到了,gilch。但是,我不知道如何使用initiate函数并将其放入Reset函数中。我是Python的初学者。有什么建议吗?
Initiate()
应该可以工作。如果您尝试了,错误是什么?它给出了错误:错误:错误简化源::函数包含异步调用但没有u参数:在第89行未定义堆栈:错误::函数包含异步调用但没有u参数:在第89行未定义,然后它给出了一组URL:()propogate就是其中之一——每个url的末尾都有不同的数字。我必须手动输入这个数字,所以我真的不想输入其他数字,除非绝对必要,否则这个错误毫无意义。尝试将
Initiate()
放在
def Initiate
函数之后。
GlowScript 2.7 VPython
#Start without running
running = False
#when "Run" is initiated
def Run(b):
    #Bring in running variable from outside
    global running
    #When Run/Pause Button is pressed, switch between running and pausing the program
    running = not running
    #When run button is pressed, change text to "Pause"
    if running: bbutton.text = "Pause"
    #When pause button is pressed, change text to "Run"
    else: bbutton.text = "Run"

#Create Run Button above program
bbutton = button(text="Run", pos=scene.title_anchor, bind=Run)

#When "Reset" is initiated
def Reset(c):
    #Bring in t and ball variables from outside
    global t, ball
    #Set back to time = 0 (when reset)
    t = 0
    #Reset ball's position to initial position (when reset)
    ball.pos = vec(0,2,0)
    #resets ball's velocity to initial velocity (when reset)
    ball.vel = vec(0,0,0)

#Create reset button above program
cbutton = button(text="Reset", pos=scene.title_anchor, bind=Reset)

#Create ground
ground = box(pos=vec(0,0,0), size=vec(10,0.2,10))
#Create ball
ball = sphere(pos=vec(0,2,0), radius=0.4, color=color.yellow)
#Initial Velocity
ball.vel = vec(0,0,0)
#Acceleration (free-falling)
g = -9.8
#Initial Time
t = 0
#Time increase per frame
deltat = 0.01

#Turns "drag" condition to false to begin
drag = False
#Initial mouse position
R = vec(0,0,0)
#When you click
scene.bind("mousedown", def():
    #Bring in drag variable from outside
    global drag
    #Set drag to true on mouseclick
    drag = True

    #When you release click
    scene.bind("mouseup", def():
        #Bring in drag variable from outside
        global drag
        #Set drag to false when you release click
        drag = False
    )
)

#While True (the program is running) and the ball is higher than slightly under the ground
#If the second condition is not there, the ball will continually go down through the ground forever
#The second condition stops the ball once it begins going underneath the platform, so it looks normal

while True and ball.pos.y > ground.pos.y + 0.48:
    #Slow down program
    rate(100)
    #When the drag condition is true (When mouse is clicked on ball)...
    if drag:
        #Set R to position of mouse
        R = scene.mouse.pos
        #Set ball position to R (position of mouse) -- allows the ball to be     dragged (changes ball position)
        ball.pos = R
    #When the program is running (run button is pressed)...
    if running:
        #Update ball velocity
        #   v      =     v0     + a (change in time)
        ball.vel.y = ball.vel.y + g*deltat
        #Update position velocity
        #   x    =    x0    +    v    *(change in time)
        ball.pos = ball.pos + ball.vel*deltat
        #When the ball hits the surface of the ground...
        if (ball.pos.y <= ground.pos.y + 0.5):
            #Change the sign of the velocity (to go back up)
            ball.vel.y = -ball.vel.y
        if (ball.pos.y < ground.pos.y +0.48):
            break
        #Update time
        t = t + deltat
def Reset(c):
    #Bring in t and ball variables from outside
    global t, ball
    #Set back to time = 0 (when reset)
    t = 0
    #Reset ball's position to initial position (when reset)
    ball.pos = vec(0,2,0)
    #resets ball's velocity to initial velocity (when reset)
    ball.vel = vec(0,0,0)

    #Initiate() -- HOW DO I RUN INITIATE INSIDE THE RESET FUNCTION**

#OTHER CODE

Initiate()    

def Initiate():
    global ground, ball, g, t, deltat, drag, R

    while True and ball.pos.y > ground.pos.y + 0.48:
        #Slow down program
        rate(100)
        #When the drag condition is true (When mouse is clicked on ball)...
        if drag:
            #Set R to position of mouse
            R = scene.mouse.pos
            #Set ball position to R (position of mouse) -- allows the ball to be dragged (changes ball position)
            ball.pos = R
        #When the program is running (run button is pressed)...
        if running:
            #Update ball velocity
            #   v      =     v0     + a (change in time)
            ball.vel.y = ball.vel.y + g*deltat
            #Update position velocity
            #   x    =    x0    +    v    *(change in time)
            ball.pos = ball.pos + ball.vel*deltat
            #When the ball hits the surface of the ground...
            if (ball.pos.y <= ground.pos.y + 0.5):
                #Change the sign of the velocity (to go back up)
                ball.vel.y = -ball.vel.y
            if (ball.pos.y < ground.pos.y +0.48):
                break
            #Update time
            t = t + deltat