1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Yakvenalex [24]
3 years ago
6

Given positive integer num_insects, write a while loop that prints that number doubled up to, but without exceeding 100. Follow

each number with a space.
Sample output with input: 8
8 16 32 64
Here's what I havenum_insects = 8 # Must be >= 1print(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?
Computers and Technology
1 answer:
grandymaker [24]3 years ago
8 0

Answer:

The while loop is executed one more time when num_insects is 64. It gets doubled and 128 gets printed. Then the while loop is not executed anymore since 128 exceeds 100.

So the reason you see 128 is because the multiplication by 2 happens inside the loop before you print. You enter it with 64, but by the time you get to the print statement, it was already multiplied.

The solution is to switch the print and the multiply:

num_insects = 8 # Must be >= 1

while num_insects <= 100 :

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

   num_insects = num_insects * 2

This has one added advantage that the very first print statement outside the loop can be removed.

You might be interested in
What is an aptitude assessment that matches students with study programs at specific colleges and universities?
melisa1 [442]
I think the correct answer would be FOCUS2. It is a system that guides members in choosing college or a major. It provides a starting point for students who are not yet certain of what they want to take academically. Hope this helps.
5 0
3 years ago
Read 2 more answers
An individualized course plan for the 4 years of
spin [16.1K]

Answer:

.....

Explanation:

8 0
3 years ago
Read 2 more answers
Free point giveaway pt.10 and brainliest to first answer
g100num [7]

Answer:

3+3=6

Explanation:

Tysm!!!

8 0
3 years ago
Read 2 more answers
Have you ever tried using such a camera?​
guapka [62]

Answer: yeah i mean i use my canon camera

Explanation:

7 0
3 years ago
Which types of customizing can you do with the Cell Style galleries? Check all that apply.
guapka [62]

Change the font size

Change the font color

Change the background color

Adjust percentage of background shade

Add a hyperlink

Explanation:

All of these have todo with style

8 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following is a template definition?
    5·1 answer
  • What are an administrator's choices for managing file permissions on a drive formatted as fat32?
    10·1 answer
  • Que funcion tiene la sombrilla forrada de aluminio​
    6·1 answer
  • What is the sum of each pair of binary integers? (assuming 8-bit register is used)
    5·1 answer
  • A star appears under the <br> menu of the word processing program.
    5·1 answer
  • What is malware? a type of virus that spreads through a network connection a type of virus that targets programs and files any p
    12·1 answer
  • Why is it important to do research prior to making a purchase? What types of sources can be helpful (or not helpful!)
    15·1 answer
  • A school has an intranet for the staff and students to use. Some of the files stored on the intranet are confidential. Give two
    15·1 answer
  • Which Excel function or tool will you use to display the cells that are referred to by a formula in the selected cell
    8·1 answer
  • Write a code snippet Now write your own code snippet that asks the user to enter two numbers of integer type (one at a time) and
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!