Python Forum

Full Version: how can i handle "expected a character " type error , when I input no character
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Add TypeError to the exception check.
key = input("enter key")
try:
    key = ord(key)
    print(key)
except (ValueError, TypeError):
    print("wrong key pressed")
Yes it worked. Thank you very much.