'I'm unable to break 'while' loop in python3

.I'm trying to run this simple command But unfortunately annoying loop I'd created. The output is unable to stop at '123(5)' rather it starting to repeat. this is the code I try to run

Please let me know where is the problem in my code. I have tried a lot but was unable to fix it



Solution 1:[1]

There are 2 problems in your code:

  1. you are not incrementing 'i' value and every time loop runs it is always 0 for loop and loop goes forever.

  2. you are using while to check input if it is 'abc' or not then you have to add a break statement so that it breaks out of loop because once code starts while loop it will not stop until you input do not changes to 'abc'.

So these are solutions:

1.If you still want to use while:-

name = input()
while name != 'abc':
  print('123')
  i = 0
  while i<5:
    for i in range(5):
      i += 1
      print('123('+str(int(i))+')')
  break

2.You should use if to apply condition:-

name = input()
if name != 'abc':
  print('123')
  i = 0
  while i < 5:
    for i in range(5):
      i += 1
      print('123('+str(int(i))+')')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashish Kumar