Please write the following code in Python 3. Also please show all output(s) and share your code.

Below is a for loop that works. Underneath the for loop, rewrite the problem so that it does the same thing, but using a while loop instead of a for loop. Assign the accumulated total in the while loop code to the variable sum2. Once complete, sum2 should equal sum1.


sum1 = 0

lst = [65, 78, 21, 33]

for x in lst:
sum1 = sum1 + x

Respuesta :

Answer:

sum2 = 0

counter = 0

lst = [65, 78, 21, 33]

while counter < len(lst):

   sum2 = sum2 + lst[counter]

   counter += 1

Explanation:

The counter variable is initialized to control the while loop and access the numbers in lst

While there are numbers in the lst,  loop through lst

Add the numbers in lst to the sum2

Increment counter by 1 after each iteration