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
If we are using an 4-character password that contains only lowercase English alphabetic characters (26 different characters), ho
Trava [24]

Answer:

11,424,400 possible passwords

Explanation:

Since all characters are letters and only lowercase

we have 26∧4 = 456,976 possibilities

For a 5-character password which is still lower case sensitive.

we have 26∧5 = 11,881,376 possibilities

Many more possible passwords = (11881376-456976)

= 11,424,400 possible passwords

6 0
3 years ago
Read 2 more answers
Define an iterative function named alternate_i; it is passed two linked lists (ll1 and ll2) as arguments. It returns a reference
zmey [24]

Answer:

answer is attached

5 0
4 years ago
If your laptop is not able to connect to your wireless network, which of the following might be a likely cause of the problem?
faltersainse [42]
C. The laptop would be too far away from the source of the signal.
7 0
4 years ago
Read 2 more answers
Pls help will give brainlest!!! The image shows a line graph. Which scientist is most likely to use this visual aid in a present
lapo4ka [179]

Answer:

D

Explanation:

6 0
3 years ago
What does it mean to clear a setting in a dialog box?
photoshop1234 [79]
It would be c because you clean in it up
3 0
4 years ago
Other questions:
  • What is the output after the following code executes?
    14·1 answer
  • For what reasons would someone use online reading tools? Check all that apply. to enhance critical-thinking skills to improve un
    5·2 answers
  • Attempting to determine a password that is not known to the attacker is often called ____.
    13·1 answer
  • What is blockchain? How it works? I heard bitcoin uses it.
    8·1 answer
  • Outline the things that Mccann needs to do right away
    13·1 answer
  • ELO 3.6 Which type of iSCSI name requires a registered domain name to generate a unique iSCSI identifier?
    12·1 answer
  • Think of an aspect of HTML that you found challenging to master. Why is that aspect of HTML important? What are some future situ
    6·1 answer
  • Difference between private and confidential data?​
    5·1 answer
  • What kind of files are automatically blocked in outlook?
    7·1 answer
  • Write an algorithm and flowchart to display H.C.F and L.C.M of given to numbers.​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!