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


Messages In This Thread
Error when entering letter/character instead of number/integer - by helplessnoobb - Jun-22-2019, 03:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,096 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  problem in entering address of a file in input akbarza 0 655 Oct-18-2023, 08:16 AM
Last Post: akbarza
  extracting x from '(x)' as an integer number Hwolff1962 3 888 Mar-23-2023, 02:50 PM
Last Post: Hwolff1962
  [solved] unexpected character after line continuation character paul18fr 4 3,419 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 3,696 May-28-2021, 09:38 AM
Last Post: faustineaiden
  Even number code syntax error MrCeez 1 2,295 May-02-2021, 06:43 PM
Last Post: Larz60+
  Error with non-ASCII character? giomach 10 5,836 Nov-09-2020, 01:13 PM
Last Post: giomach
  syntaxerror when entering a constructor MaartenRo 2 1,995 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,187 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Excpetion Handling Getting Error Number gw1500se 4 2,392 May-29-2020, 03:07 PM
Last Post: gw1500se

Forum Jump:

User Panel Messages

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