Q. Figure out the problem with following loop(s).

[Hint: Both loops have similar problem]:

(a)
a = 10
while (a < 100):
    print (a)
print("Over!")

(b)
key = 'panda'
correct = False
while not correct :
    inp = input("Enter secret key :")
    if inp == secret :
        print ("CORRECT! You got that.")
    else:
        print ("Wrong guess. Try again.")

Suggest the solution. Correct the code and run to check the output.
Problem with both the codes above :-
Solution for (a) (correct code) :-
Solution for (b) (correct code) :-


Answer :-

Problem with both the codes above :- Both problems have not update expression.

Correct code for (a):-
a = 10
while (a < 100):
    print (a, end = ", ")
    a += 1
print("Over!")
Output :-

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, Over!
>>>

Correct code for (b):-
key = 'panda'
correct = int (input ("Enter How many attempt do you want? :- "))
while correct > 0 :
    inp = input("Enter secret key : ")
    if inp == 'secret' :
        print ("CORRECT! You got that.")
        break
    else:
        print ("Wrong guess. Try again.")
    correct -= 1
Output :-

Enter How many attempt do you want? :- 5
Enter secret key : sdfg
Wrong guess. Try again.
Enter secret key : cvbvn
Wrong guess. Try again.
Enter secret key : secret
CORRECT! You got that.
>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post