Python Forum
how can i handle "expected a character " type error , when I input no character
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can i handle "expected a character " type error , when I input no character
#1
key = input("enter key")
try:
    key = ord(key)
except ValueError:
    print("wrong key pressed")
Error:
enter key Traceback (most recent call last): File "/home/vivek/Documents/prac.py", line 3, in <module> key = ord(key) TypeError: ord() expected a character, but string of length 0 found
Reply
#2
Add TypeError to the exception check.
key = input("enter key")
try:
    key = ord(key)
    print(key)
except (ValueError, TypeError):
    print("wrong key pressed")
Reply
#3
Yes it worked. Thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,515 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Wrong type error rowan_bradley 6 1,144 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
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,604 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Custom method to handle exceptions not working as expected gradlon93 3 944 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  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
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,304 Sep-26-2022, 10:09 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

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