Bottom Page

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
Quote
#2
Add TypeError to the exception check.
key = input("enter key")
try:
    key = ord(key)
    print(key)
except (ValueError, TypeError):
    print("wrong key pressed")
Quote
#3
Yes it worked. Thank you very much.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Input Error Dream 0 32 7 hours ago
Last Post: Dream
  Remove from end of string up to and including some character lbtdne 2 292 May-17-2020, 09:24 AM
Last Post: menator01
  Getting an error while using input function dcsethia 5 192 May-11-2020, 04:59 PM
Last Post: buran
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 402 Apr-29-2020, 06:41 AM
Last Post: buran
  Data Type conversion error rajeevjagatap 2 323 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Help ANSI character and python... sid4g 2 249 Mar-19-2020, 06:08 PM
Last Post: sid4g
  Exception: Returned Type Mismatch Error devansing 1 373 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  loop only prints last character. mcmxl22 1 222 Feb-17-2020, 02:36 AM
Last Post: menator01
  Python Type error! SamAnw 4 359 Feb-14-2020, 03:46 PM
Last Post: buran
  create a list of object with a list of character studenthch 0 197 Feb-12-2020, 08:43 AM
Last Post: studenthch

Forum Jump:


Users browsing this thread: 1 Guest(s)