Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
catch input type error
I am trying to get this script to only accept integers.
number = int(input("Enter a number. "))
if isinstance(number, str):
    print("Entry must be a number")
I have also tried:
if type(number) is str:
    print("Entry must be a number")
I enter a and keep getting:
ValueError: invalid literal for int() with base 10: 'a'
Why dosen't this catch the error?
input() returns you a string and you need to check if all characters in that string are in '0123456789'
as only then int() can convert that string into a number.
import re
number = input("Enter a number: ")
if not re.match("^[0-9]*$", number):
    print("Entry must be a number")
You can put this in a while loop to continue asking for input until it accepts the syntax. Was in a similar situation a few days ago and a quick google search got me the answer. You should also download the Python pdf on your phone or comp :)
(Aug-10-2019, 06:21 AM)mcmxl22 Wrote: Why dosen't this catch the error?

Because input is throwing an exception (i.e. the ValueError) and you don't have any exception handling code (i.e. a try and catch).

Note that throwing is not the same as returning - when an exception is thrown, the function is exited in a different way. No value is returned, so you have to handle the failure in a different way (that is, using the exception handling mechanism). This is one reason one might choose to work in a more functional language - if the functions are pure and so can only return values, then there's only a single flow of execution and handling failures just sits inside that like everything else.
(Aug-10-2019, 07:23 AM)ndc85430 Wrote:
(Aug-10-2019, 06:21 AM)mcmxl22 Wrote: Why dosen't this catch the error?
Because input is throwing an exception

No, it´s the int() function that throws the exeption as the string 'a' cannot be converted into an integer number.
Quote:ValueError: invalid literal for int() with base 10: 'a'
Just convert the input string to an integer and catch the error if there is any.
You can look at str.isdigit() and str.isnumeric() methods as well
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyspark SQL Error - mismatched input 'FROM' expecting <EOF> Ariean 3 585 Nov-20-2020, 03:49 PM
Last Post: Ariean
  try catch question ,get data from main code korenron 7 250 Nov-03-2020, 09:28 AM
Last Post: korenron
  EOF error while taking input ShishirModi 1 339 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  Type Error or Value Error? spalisetty06 3 390 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  Input Error Dream 2 520 Jul-12-2020, 05:41 PM
Last Post: bowlofred
  Getting an error while using input function dcsethia 5 499 May-11-2020, 04:59 PM
Last Post: buran
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 1,322 Apr-29-2020, 06:41 AM
Last Post: buran
  Data Type conversion error rajeevjagatap 2 760 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Exception: Returned Type Mismatch Error devansing 1 1,041 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Python Type error! SamAnw 4 656 Feb-14-2020, 03:46 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020