omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Cool number animation question

    Pythonista
    3
    3
    2087
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • janAaron
      janAaron last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • Dann239
          Dann239 last edited by

          A couple things. Your two loops. <code> while 100: </code> 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
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors