0

I want this python code comes out from loop when I enter 0 as input number of "num" variable But it continues printing these three lines constantly


Thanks

num = 10     #10 is dummy number for starting loop 
while(num!=0):
    print("1)Test func1")
    print("2)Test func2")
    print("0)Exit")
    num = input("Enter a number:")

print("Comes out from while loop!")
MohmSh
  • 3
  • 1

3 Answers3

3

There reason behind is that the input takes input as a string and you have to either convert it to int:

num = int(input("Enter a number:"))

or change the while loop:

while(num!='0'):
Marcus.Aurelianus
  • 1,520
  • 10
  • 22
0

print takes input as string , in order to come out of loop use num = int(input("Enter a number:"))

it will consider num variable as an integer but not an string and will come out of the loop.

rachit17
  • 24
  • 3
-1

You have got a correct answer before this but you can change your code too:

while True:
    print("1)Test func1")
    print("2)Test func2")
    print("0)Exit")
    num = input("Enter a number:")
    if num == '0':
        break

print("Comes out from while loop!")

at the begining while always get True, you don't need to set value, 'break' means 'stop current loop and exit'

Dawid Żurawski
  • 519
  • 1
  • 5
  • 18