from math import *
from graphics import *
from time import *
def main():
veloc = .5 #horizontal velocity (pixels per second)
amp = 50 #sine wave amplitude (pixels)
freq = .01 #oscillations per second
#Set up a graphics window:
win = GraphWin("Good Sine Waves",400,200)
win.setCoords(0.0, -100.0, 200.0, 100.0)
#Draw a line for the x-axis:
p1 = Point(0,0)
p2 = Point(200,0)
xAxis = Line(p1,p2)
xAxis.draw(win)
#Draw a ball that follows a sine wave
for time in range(1000):
amp = amp * 2
x = time*veloc
y = amp*sin(freq*time*2*pi)
#y = abs(amp*sin(freq*time*2*pi))
ball = Circle(Point(x,y),2)
ball.draw(win)
sleep(0.1) #Needed so that animation runs slowly enough to be seen
#win.getMouse()
#win.close()
main()
我遇到的问题是试图在 for 循环中缓慢减少 amp 变量。 amp 变量设置为 50。我知道为了减少它应该像这样:
amp = amp
amp = amp / 2
但每次我在 for 循环中尝试这些语句时,它都不起作用。
最佳答案
for i in range(0,10):
amp= amp/2
这打印
25.0
12.5
6.25
3.125
1.5625
0.78125
0.390625
0.1953125
0.09765625
0.048828125
>>>
关于python - 在 Python 中操作变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21946774/