0

3. Convert the following for loop to while loop

    p=0
    for k in range (5,19,3):
        print("k=",k)
        p+=k
    else:
        print(k*2)
    print(p)

Output

k= 5
k= 8
k= 11
k= 14
k= 17
34
55

My while loop

def q3():
        k=5
        p=0
        while p<=55:
            print("k=",k)
            k+=3
            p+=k
        else:
            print(k*2)
        print(p)

Output

k= 5
k= 8
k= 11
k= 14
k= 17
40
70  

Question.

In the last two lines of the for-loop my output for k x 2 was 34 (last k value generated=17 x 2) print(p) was 55 (sum of all k values generated)

Whereas the last two lines of my while-loop produced k x 2: 40 (Why is it generating 40?) print(p) was 70 (Again, why is it producing 70?)

2
  • 1
    k+=3 is executed finally in the while-loop even if it is exited before the next iteration. Nothing similar happens in the for-loop. Oct 25, 2020 at 5:20
  • That makes great sense. Thank you! Oct 25, 2020 at 6:41

2 Answers 2

0

As Micheal mentioned the last k +=3 statement in the while loop will be executed and then it will exit the loop. You need to either subtract your limit (i.e. 19) and initial value of k (i.e. 5) by 3, or reverse the last +3 operation by k -=3.

Solution 1:

p=0                                                                                                                          
k=2                                                                                                                          
while k < 16:                                                                                                                       
    k += 3                                                                                                                    
    p += k                                                                                                                   
    print("k =",k)                                                                                                           
else:                                                                                                                        
    print(k*2) 
print(p)

Solution 2:

p=0                                                                                                                          
k=5                                                                                                                          
while k < 19:                                                                                                                       
    print("k =",k)
    p += k                                   
    k += 3                                                                                                                                                                                                                           
else:   
    k -= 3                                                                                                                     
    print(k*2) 
print(p)
0
0

after ending the loop in for loop the counter always is the last acceptable number which is 17, but in while loop we added 3 to the counter manually so after ending while loop the counter has its last modification value which is 20. that's why we should decrease k by three in line print(k*2). also the line else: is redundant, so you can omit it.

print("for version")
p=0
for k in range (5,19,3):
    print("k=",k)
    p+=k
print(k*2)
print(p)

print("while version")
p=0
k=5
while k<19:
    print("k =",k)
    p += k
    k += 3
print((k-3)*2)
print(p)
2
  • you are welcome, don't forget to mark it as an accepted answer Oct 25, 2020 at 6:44
  • 1
    It would even be more valuable if you would write a few explaining words. Oct 25, 2020 at 7:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.