Question

Q4) Code Analysis (10 Points) What do the following loops print? Work out the answer by tracing the code, not by using the computer a. (2 Points) b. (2 Points) c. (2 Points) for n in range (1, 6): for n in range(1, 11): for n in range(1, 6): n=n+2 print(s) print(s) print (s, n) Rewrite the following for loop as a while loop (4 Points). fori in range(1, 10): Instructions Use only the concepts from Chapter 2 thru 5 Python
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, you will need to understand the meaning of range function to answer this,

range(start ;stop; step) which says interate from start till stop(excluding stop) incrementing each time by step

which is roughly equivalent to for(i=start;i<stop;i=i+step)

1. so in 1st question, no step is given, default is 1.

so the loop prints 5 numbers (1-6) for n=1,2,3,4,5

n=1: s=s+n=1+1= 2

n=2: s=s+n=2+2= 4

n=3: s=s+n=4+3= 7

n=4: s=s+n=7+4= 11

n=5: s=s+n=11+5= 16

2.this is almost same as 1st question, but the interesting part is loop variable is updated inside the loop,

but in pyhton, for loop iterates over all the numbers in range() so even if n was updated as soon as we go for next iteration it is assigned the next value from range(1,11). hence it prints,

4
8
13
19
26
34
43
53
64
76

3. this one follows same concept as 2 and infact is exactly same as question 1 output wise because n has the same range, only difference being print is outside of for loop, so only printed once,

hence prints (16,6)

Now, to tackle the above situations we can use while loops,

we are asked to rewrite

s=0

for i in range(1,10):

s=s+i

which essentially adds in incremntal value to s and finally at 9 we get s= 46

with while, we explicitly have to declare i and increment it like this,

s=1
i=1
while i<10:
    s=s+i
    i+=1
    print(s)

Thumbs up if this was helpful, otherwise let me know in comments. Good day.

Add a comment
Know the answer?
Add Answer to:
Python What do the following loops print? Work out the answer by tracing the code, not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • rewrite this post and code their for loops as while loops. The only thing I need...

    rewrite this post and code their for loops as while loops. The only thing I need to see in the while loop body is a statement that would change the loop counter as the for loop statement would. for (cars = 70; cars < 86; cars++) { } for (planes = 75; planes >= 0; planes--) { } for (trains = 0; trains < 100; trains += 4) { } Part 2: I used my third for loop do {...

  • using python 1.   Translate the following FOR loops to equivalent WHILE loops: a. for count in...

    using python 1.   Translate the following FOR loops to equivalent WHILE loops: a. for count in range(100): print(count) b. for count in range(1, 101): print(count) c. for count in range(100, 0, –1): print(count)

  • python programming Which of the following code will print all odd numbers between 3 and 11,...

    python programming Which of the following code will print all odd numbers between 3 and 11, each number separated by a space as shown below 35 7 9 11 for i in range(3, 11, 1): if 1%2 ID: print(i, end = " for i in range(3, 10, 2): print(i, end = for i in range, 11, 2): if 152 == 0: print(i, end = "") for i in range 12, 1): print(i, end = "")

  • In Python! Create the program that print out only odd numbers 1,3,5,7,9 Use either a while...

    In Python! Create the program that print out only odd numbers 1,3,5,7,9 Use either a while loop or a for loop to print only odd numbers 1, 3, 5, 7, 9 *tip: you can use range () function, or a condition with ‘break’ (e.g, while count < 10) In python!: Create a program that print out 12 months of a year and associated numbers (e.g., January 1, February 2…). Two lists should be created, and then loop through the list...

  • Python question. How do i make this code work? im trying to print the value of...

    Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value)    def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Starting out with Python 4th edition I need help with while loops I can't even get...

    Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY       APPROXIMATE POPULATION ------------------------------------------------- 1            2 2            2.600 3            3.380 4            4.394...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Write a section of Java code using while, do-while and for loops Write a section of...

    Write a section of Java code using while, do-while and for loops Write a section of Java code that will print out random integers between 1 and 100 while N is less than or equal to 5. Sample output > random # 1 is: 73 random # 2 is: 68 random # 3 is: 76 random # 4 is: 64

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT