Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
catch input type error
#1
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:
Error:
ValueError: invalid literal for int() with base 10: 'a'
Why dosen't this catch the error?
Reply
#2
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.
Reply
#3
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 :)
Reply
#4
(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.
Reply
#5
(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'
Reply
#6
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."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,142 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,067 Jul-22-2023, 11:33 PM
Last Post: jhancock
  try catch not working? korenron 2 811 Jan-15-2023, 01:54 PM
Last Post: korenron
  how to make sure an input is from the wanted type astral_travel 16 2,372 Oct-31-2022, 04:11 PM
Last Post: astral_travel
  Error in Using INPUT statement gunwaba 1 2,015 Jul-03-2022, 10:22 PM
Last Post: deanhystad
  Python Anytree - Is not of type 'NodeMixin' error georgebijum 3 2,026 May-05-2022, 01:43 PM
Last Post: Gribouillis
  Multiprocessing queue catch get timeout Pythocodras 1 2,237 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,931 Oct-06-2021, 09:39 AM
Last Post: menator01
  twisted: catch return from sql wardancer84 0 1,500 Sep-08-2021, 12:38 PM
Last Post: wardancer84
  how to catch schema error? maiya 0 1,808 Jul-16-2021, 08:37 AM
Last Post: maiya

Forum Jump:

User Panel Messages

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