Bottom Page

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?
Quote
#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.
Quote
#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 :)
Quote
#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.
Quote
#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'
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Type Error: bad operand type for unary +: 'str' Psypher1 2 972 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  Getting type from input() function in Python 3.0 leodavinci1990 7 367 Jul-29-2019, 08:28 PM
Last Post: avorane
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 1,353 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  How to catch string between two strings SriMekala 3 361 May-24-2019, 03:05 AM
Last Post: heiner55
  Type error when reading in different data types on an __init__ method Dylanmull 3 320 May-09-2019, 02:05 PM
Last Post: buran
  Getting error "Type error-a bytes-like object..." mrapple2020 1 1,353 Apr-06-2019, 06:37 PM
Last Post: mrapple2020
  scatter plot error - possibly data type - TypeError: nan is not a string chudson 1 1,156 Mar-24-2019, 11:48 AM
Last Post: chudson
  Unexpected expected type error result MartinMaker 1 352 Feb-16-2019, 05:02 PM
Last Post: micseydel
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 313 Feb-07-2019, 03:15 PM
Last Post: coltron1282
  inserting input gives me error message RubberNuggets 3 353 Jan-15-2019, 06:17 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)