0

I've created a Python script to get the mobile number from user

I need to get the number as integer

So I used,

Mobile=int(input("Enter the Mobile Number: "))

While running my program, if entered any character instead of integer, the program crashes and results in an error message.

But, my need is, it have to give the error message and have to run again for asking mobile number.

let me know the way please....!!

Pavel
  • 7,436
  • 2
  • 29
  • 42
Dj'hemath
  • 17
  • 6

1 Answers1

1
while True:
    given = input("Enter the Mobile Number: ")
    try:
        mobile = int(given)
        break
    except Exception:
        print("Invalid input, should only contain numbers")

print("entered number is", mobile)
johnII
  • 1,423
  • 1
  • 14
  • 20