Home > Net >  How to exit the program after max 3 attempts using Python, for exception program , if you dont get t
How to exit the program after max 3 attempts using Python, for exception program , if you dont get t

Time:09-22

How to exit the program after max 3 attempts using Python, for exception program , if you dont get the desired output?

while True:
         try:
            x = int(input("Please enter a number: "))
            break
         #except Exception as e:
         #     print (e)
         except ValueError:
             print ("You have entered the non-numeric value. Enter the numerical value.")
         except KeyboardInterrupt:
             print ("\nYou have press Ctr C.")
             exit (1)

CodePudding user response:

nothing = 0
while nothing < 3:
    nothing  = 1
    try:
       x = int(input("Please enter a number: "))
       break
         #except Exception as e:
         #     print (e)
    except ValueError:
        print ("You have entered the non-numeric value. Enter the numerical value.")
    except KeyboardInterrupt:
        print ("\nYou have press Ctr C.")
        break

CodePudding user response:

You want exit after 3 attemps. Try this and count that input is wrong:

num_err = 0
while num_err < 3:
    try:
        x = int(input("Please enter a number: "))
    except ValueError:
        print ("You have entered the non-numeric value. Enter the numerical value.")
        num_err  = 1
    except KeyboardInterrupt:
        print ("\nYou have press Ctr C.")
        break

CodePudding user response:

Try:

c = 0
while c < 3:
     c  = 1
     try:
        x = int(input("Please enter a number: "))
        break
     except ValueError:
         print ("You have entered the non-numeric value. Enter the numerical value.")
     except KeyboardInterrupt:
         print ("\nYou have press Ctr C.")
         break

CodePudding user response:

Use sys.exit() for stopping the whole script

import sys

while True:
     try:
        x = int(input("Please enter a number: "))
     #except Exception as e:
     #     print (e)
     except ValueError:
         print ("You have entered the non-numeric value. Enter the numerical value.")
     except KeyboardInterrupt:
         print ("\nYou have press Ctr C.")
         sys.exit()

CodePudding user response:

I read all the code that is submitted above but one thing is missing, that if a user enter two incorrect value then enter a correct value after that he has only one chance. That is if user enter any wrong input after entering two correct input the loop will break.

So I have tried to solve this problem. Look at my code...

count = 0
while True:
    try:
        x = int(input("Please enter a number: "))
        count = 0
        break
    except ValueError:
        count  = 1
        print("You have entered the non-numeric value.Only three invalid inputs are allowed. Enter the numerical value.")

    except KeyboardInterrupt:
        print("\nYou have press Ctr C.")
        exit(1)
    if count == 3:
        break

I am trying to explain what I have written in this code. As you have mentioned that a user can enter only three invalid input so I have increased the variable count each time when user will enter an invalid input. But if a user will enter a valid input then the count will be 0. It means again the user can enter maximum three invalid input. After entering three invalid input continuously the count will be 3 and the loop break

  • Related