+ 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()
8 Respuestas
+ 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.
+ 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.
+ 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()
+ 1
Thanks mate I actually put i=i+1 under while loop so it keeps on printing.....silly mistake 🤗
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 ??
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??
0
thanks mate🤗👍