Forum Archive

Cool number animation question

janAaron

I made a program that spits out hello + some numbers, and it makes a really cool animation. I tried to make it so that when the random number reaches 20, it changes the color of the console output, but it didn't work. What's wrong?

import console
console.set_color()
a = 1
while 100:
    print ("hello "+ str(a+a))
    a +=a

if a == 20:
        console.set_color(1.00, 0.00, 0.00)

Thank you!

omz

Two reasons:

  1. The if statement where you set the color is never reached because while 100: will repeat forever (it's basically the same as while True) – though it looks like the indentation might have been lost when you pasted the code here, so this point might not actually apply, but:

  2. a will never be equal to 20. In every iteration of the while loop, the value is doubled, which means that the values of a will be 1, 2, 4, 8, 16, 32, 64... but never 20.

Dann239

A couple things. Your two loops. while 100: will always loop and never break it is a true statement. It needs a point of reference. So you would say that while something is below or above or not equal to this integer, do the next steps. Your if statement is outside the previous loop. If the first loop were to execute as you would want, a would never equal twenty when the first loop decided to break. So I incorporated the if loop in the while loop to catch the number twenty. There are probably prettier ways of doing this. But here is my answer:

import console
from random import random<p>
a = 1
while a < 100:
    if a == 20:
        console.set_color(random(), random(), random())
    else:
        console.set_color()
    print('hello '+str(a))
    a += 1