I am running simple program in python. I am using input method and using following code
overflow = int(input("Enter the number"))
print(overflow)
I wanna that if someone will enter sting in input then input will show please enter number only
I am running simple program in python. I am using input method and using following code
overflow = int(input("Enter the number"))
print(overflow)
I wanna that if someone will enter sting in input then input will show please enter number only
This is a good place to put a try-except
block and learn about exception handling.
try:
overflow = int(input("Enter the number"))
print(overflow)
except ValueError:
print("Please enter numbers only.")
try-except
blocks are pretty cool, in the sense that you force an error, then catch it (that's what the except
block does).
ValueError
exceptions will be...
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
You can wrap this around a while
block to prevent the code from automatically closing or carrying on.
Hope this is what you want
overflow = input("Enter the number")
try:
overflow = int(overflow)
print(overflow)
except ValueError:
print("Please enter number only")