在Python中操作变量
我遇到的问题是试图在for循环中缓慢降低amp变量。amp变量设置为50。我知道为了减少它应该是这样的:在Python中操作变量,python,algorithm,variables,Python,Algorithm,Variables,我遇到的问题是试图在for循环中缓慢降低amp变量。amp变量设置为50。我知道为了减少它应该是这样的: 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 #oscil
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
amp = amp / 2
致:
这张照片
for i in range(0,10):
amp= amp/2
为什么不列一个清单?“它不起作用”-会发生什么?你能展示一下代码吗?
amp=amp*2
-是我一个人,还是你在做与你想做的完全相反的事情?*=2
有一个同样快速增长的问题。
for time in range(1000):
amp /= 2 #this line now divides
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)
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
>>>