-2

I'm trying to create a loop for the given problem. I need help; it's not printing the way it should.

Given positive integer num_insects, write a while loop that prints that number doubled without exceeding 100. Follow each number with a space.

Ex: If num_insects = 8, print:

8 16 32 64

Here's what I have

num_insects = 8 # Must be >= 1

print(num_insects, '', end='')

while num_insects <= 100 :

     num_insects = num_insects * 2

     print(num_insects,'', end="")

This code prints the number 128 even thought the loop is set to end after 100? Why is that?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
pcguy1045
  • 1
  • 1
  • 1
  • 2

7 Answers7

4

You want to multiply num_insects after you print out the result. You can pass in an empty string to the end parameter as stated in matt's answer:

num_insects = 8
while num_insects <= 100:        
  print(num_insects,'', end="")
  num_insects = num_insects * 2
print("") # newline

Output:

8 16 32 64 
Linus
  • 1,516
  • 17
  • 35
  • I put new code up top, any reason why 128 would still be printed? – pcguy1045 Sep 22 '16 at 22:36
  • @pcguy1045 ser my answer, you have to put `num_insects = num_insects * 2` after you primt out num_insects otherwise the condition will be true before we multiply by two. This solves both of your problems. – Linus Sep 22 '16 at 22:43
  • using sys to print out is a little overkill... the OP is doing that part right... – Copperfield Sep 22 '16 at 22:44
  • @Copper right, passing in the end parameter with an empty string is better and easier in this case. Going to update my answer. – Linus Sep 22 '16 at 22:45
2

the print function implicitly adds a newline: https://docs.python.org/2/library/functions.html#print

You can pass an alternative ending with the end = argument; try passing None or ' ' and see if that gets you a result that you like better.

mattbornski
  • 11,895
  • 4
  • 31
  • 25
  • It prints the number 128. I have the loop set to end when it reaches 100. How do I prevent that? Also is there a way to print the original input before it gets doubled? – pcguy1045 Sep 22 '16 at 22:27
2

This is all you need without adding too much but also letting it print out the way it needs to.

num_insects = 8 # Must be >= 1

while num_insects <= 100:
    print(num_insects, end=' ')
    num_insects = num_insects * 2
0

Wow! I actually have something to add. I too was getting the answer 128 when I used the above code, so what I did is use the break command. This is the code I used, and it seemed to work:

    num_insects = 8 # Must be >= 1
    print(num_insects, end=' ')
    while num_insects < 100:
        num_insects = num_insects * 2
        if num_insects <= 100:
            print(num_insects, end=' ')
        else:
            break
Mark Reel
  • 1
  • 1
0

You probably don't need this answer anymore, but I solved it by doing this:

  num_insects = int(input())
  while num_insects <= 100:
      print(num_insects, '', end='')
      num_insects = num_insects * 2

It is similar to other answers, but unlike the others there is no print("") #newline at the end. The '', helps ensure a space is after each number. end='' helps ensure it all prints on the same line, since print() adds a newline each time. There is also no if/else or break point. Instead of setting a fixed value I allowed the user to enter a varying amount of starting "bugs". If you wanted it to print more generations of offspring after 100 just change it the 100 to something else. 1000 for example will give you 7 generations of off spring but exclude the 8th because the 8th generation has over 1000 bugs.

0
num_insects = int(input('Enter num: '))
while (num_insects > 0) and (num_insects <= 100):
   print(num_insects, end='\n') # New line
   num_insects = num_insects * 2

Use end='\n' to get new line.

tdy
  • 36,675
  • 19
  • 86
  • 83
  • When you add an answer to an old question with many answers, like this one, please make sure that you are providing a solution that is better than existing solutions or that explains it better. It's not clear what you're trying to add to existing answers here. – joanis Oct 31 '21 at 15:39
  • Back to original question "Given positive integer num_insects, write a while loop that prints that number doubled without exceeding 100. Follow each number with a space." To detect range non-negative and is less than or equal 100 the WHILE loop below – Peter Truong Oct 31 '21 at 16:59
0

The problem isn't that you have the wrong code, it's that you aren't putting the while loop in the correct order. You want to print first every round so that way when your number does reach the 128 it ends the iteration.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '22 at 14:38