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
  EOF error while taking input ShishirModi 1 214 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  error "IndentationError: expected an indented block" axa 4 347 Sep-08-2020, 02:09 PM
Last Post: ibreeden
  Iterating over word vs. character Mark17 6 272 Aug-11-2020, 04:56 PM
Last Post: Mark17
  Type Error or Value Error? spalisetty06 3 264 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 289 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Input Error Dream 2 331 Jul-12-2020, 05:41 PM
Last Post: bowlofred
  Remove from end of string up to and including some character lbtdne 2 431 May-17-2020, 09:24 AM
Last Post: menator01
  Getting an error while using input function dcsethia 5 352 May-11-2020, 04:59 PM
Last Post: buran
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 827 Apr-29-2020, 06:41 AM
Last Post: buran
  Data Type conversion error rajeevjagatap 2 530 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap

Forum Jump:

User Panel Messages

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