Home > Enterprise >  it doesn't loop but just breaks even if i input Y
it doesn't loop but just breaks even if i input Y

Time:12-18

def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
            num_two = int(input('Do you want to play again n/Y:'))
            
        except:
             if num_two == "y":
                continue 
        finally:
            print("Goodbye")
            break

CodePudding user response:

def interact(): 
   while True:
       try:
          num = int(input("Please input an integer: ")) 
          if (num % 2) == 0: print ("{0} is even".format(num))
          else: print("{0} is odd".format(num))
          num_two = int(input('Do you want to play again n/Y:'))
       except:
          if num_two == "y":
             continue 
       finally:
          print("Goodbye")
          break

the problem with this code is that the 'break' makes it so that you are out of the while true loop (i think)

therefore i think removing the 'break' might solve it?

CodePudding user response:

You must add specific error to except and remove break from finally:

try:
    user_input = input()
    int(user_input)
except ValueError:
    print('not a number')
    if user_input = 'y':
        continue
    else:
        break
finally:
    print('Finnaly')
  • Related