Hey Can anyone tell me why its continuous printing after number matches as my while loop has not true condition?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hey Can anyone tell me why its continuous printing after number matches as my while loop has not true condition??

def abc(): i = 0 x = 5 y = 11 while i<4: if x == y: print("number match") else: print("number dont match") i = i+1 x = x+1 y = y-1 abc()

10th May 2020, 1:34 PM
Tejas Joshi
Tejas Joshi - avatar
7 Answers
+ 2
Are you sure? This code terminates just fine! def abc(): i = 0 x = 5 y = 11 while i<4: if x == y: print("number match") else: print("number dont match") x = x+1 y = y-1 i = i+1 abc() I think you accidentally placed it outside the while loop.
10th May 2020, 1:54 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
i=0, x=5, y=11, don't match i=1, x=6, y=10, don't match i=2, x=7, y=9, don't match i=3, x=8, y=8, match, so i is not increased anymore. It stays smaller than 4 forever. If you want the loop to stop, you can put the i=i+1 outside the if/else condition, at the end of the loop.
10th May 2020, 1:43 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
There's no need for that here, but you could do something like def abc(): i = 0 x = 5 y = 11 while True: if x == y: print("number match") break else: print("number dont match") x = x+1 y = y-1 i = i+1 abc()
10th May 2020, 2:01 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Thanks mate I actually put i=i+1 under while loop so it keeps on printing.....silly mistake 🤗
10th May 2020, 1:57 PM
Tejas Joshi
Tejas Joshi - avatar
0
yes I tried to put the i=i+1 outside if else now it's continuous printing after number match that it does not match......where do I add break ??
10th May 2020, 1:53 PM
Tejas Joshi
Tejas Joshi - avatar
0
hey can you please also tell me If i make the condition true and wants to use break where will I put that in??
10th May 2020, 1:59 PM
Tejas Joshi
Tejas Joshi - avatar
0
thanks mate🤗👍
10th May 2020, 2:03 PM
Tejas Joshi
Tejas Joshi - avatar