从for循环打印一次迭代

时间:2017-09-23 06:38:56

标签: python for-loop

我现在正在使用for循环并且假设糖果可以某种方式自己加倍,它总是以一个糖果开始并且在1秒内以y = ax + b的函数相乘并且导致y值结转到时间= 2秒。例如:

a = 3 #this is a in ax+b
b = 1 #this is b in ax+b
t = 3 #this is time in seconds

x = 1
for t in range(t):
    t+=1
    x=(a*x)+b
    print('str(x) + ' candies after '+str(t)+' seconds')

这段代码给了我三个输出,

4 candies after 1 seconds
13 candies after 2 seconds
40 candies after 3 seconds

这些数字是正确的,但问题是我只想打印最后一个声明。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以通过将for语句移出for循环

来完成此操作
// set up view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,eyeX+directionX,eyeY+directionY,eyeZ+directionZ,upX,upY,upZ);

// save view matrix
glPushMatrix();

// add model matrix
glRotated(45,0,0,1);
glTranslated(0,0,50);

// do the drawing
glColor3d(1,1,1);
glDisable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D,texture2);
GLUquadric* params = gluNewQuadric();
gluQuadricDrawStyle(params,GLU_FILL);
gluQuadricTexture(params,GL_TRUE);
gluSphere(params,100,20,20);
gluDeleteQuadric(params);
glEnable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D,texture1);
glCallList(tie); 
glEnd(); // <-- maybe this could be done in "tie"

// restore the view matrix
glPopMatrix();

您也可以将变量t中的一个更改为其他变量。 例如。

for t in range(t):
    t+=1
    x=(a*x)+b
print(str(x) + ' candies after '+str(t)+' seconds')
# 40 candies after 3 seconds