Can I use a loop to print the binary numbers 2 through 5?

Question

Can I use a loop to print the binary numbers 2 through 5?

Answer

Yes! It’ll save you the agony of writing the same line of code 4 times, if nothing else. To print each number in a loop, just be sure you’re not adding any additional text or unnecessary line breaks. The instructions want each number on a new line by itself.
For example, in pseudo code what we’d do is:

for numbers 2 through 5:
  print binary of current number
1 Like

The example is psuedo code, so running the code will of course give an error

thanks alot.

i got it now.

:slight_smile:

for x in range(2, 5, 1):
print bin(x)

5 Likes

Great! But the step isn’t necessary…

2 Likes

actually:

for x in range(2, 6):
print bin(x)