Python Forum
Error when entering letter/character instead of number/integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when entering letter/character instead of number/integer
#1
Hello all,
Excuse my complete lack of knowledge and "newbie-ness" as it has only been a few hours since I started my already rocky road to understanding Python.
I am going through some beginner's tutorials available on web and came up with "identification"-type of codes below:

The "ID number" or "idn" has to be a number between 0 and 10.

print("What is your ID number, " + str(name) + "?", end="  ")
idn=input()
while True:
    if int(idn) > 10 or int(idn) < 0:
        print("     ERROR: the ID number is a number between 0 to 10.", end="  ")
        idn=input()
        continue
    else:
        print("Thank you, no. " + str(idn))
        input()
        break
The code seemed to be working fine, but when I just got curious and entered a character/alphabet/word instead of a number, it gave me an error message. I'd like to show "ERROR: ID is a number, not a character," if a character/word is entered. Here is my try, but it has been unsuccessful so far.

print("What is your ID number, " + str(name) + "?", end="  ")
idn=input()
while True:
    if int(idn) > 10 or int(idn) < 0:
        print("     ERROR: the ID number is a number between 0 to 10.", end="  ")
        idn=input()
        continue
    if idn=str:
        print("     ERROR: ID is a number, not a character,", end="  ")
        idn=input()
        continue
    else:
        print("Thank you, no. " + str(idn))
        input()
        break
That "if idn=str:" is what I am struggling with. How can I set up that if-command? Thank you for your time! I'd really appreciate any advice!
Reply
#2
helplessnoobb Wrote:it gave me an error message.
This is what you need to concentrate on. Python's error messages usually contain critical information about how to solve the issue.

After idn = input(), the value of idn is necessarily of type str which you can check with print(isinstance(idn, str)), which should print True. The best thing to do here is to convert to int and print an error if it is not possible
while True
    idn = input("What is your ID number? ")
    try:
        n = int(idn):
    except ValueError:
        print("Please enter a number, not", repr(idn))
        continue
    if n in range(11):
        print('Thank you, your ID number is ', n)
        break
    else:
        print('Bad value, try again')
        continue
Reply
#3
You have to check directly after idn=input() what you get back so what is idn.
Your "if idn=str:" is complete nonsense, you really should work through a Python tutorial.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 0 214 Aug-30-2020, 02:35 PM
Last Post: akanowhere
  syntaxerror when entering a constructor MaartenRo 2 302 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  SyntaxError: unexpected character after line continuation character siteshkumar 2 309 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Excpetion Handling Getting Error Number gw1500se 4 315 May-29-2020, 03:07 PM
Last Post: gw1500se
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 432 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Why is this an error? (string and integer) pcsailor 4 1,137 Feb-05-2020, 08:26 AM
Last Post: DeaD_EyE
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 547 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to Stop Sentinel Value from Entering Final Output ZQ12 3 566 Nov-11-2019, 07:25 AM
Last Post: perfringo
  check if the number is a prime integer atlass218 5 625 Sep-26-2019, 07:58 AM
Last Post: atlass218
  Replace changing string including uppercase character with lowercase character silfer 11 1,681 Mar-25-2019, 12:54 PM
Last Post: silfer

Forum Jump:

User Panel Messages

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