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
  loop only prints last character. mcmxl22 1 103 Feb-17-2020, 02:36 AM
Last Post: menator01
  Python Type error! SamAnw 4 128 Feb-14-2020, 03:46 PM
Last Post: buran
  create a list of object with a list of character studenthch 0 72 Feb-12-2020, 08:43 AM
Last Post: studenthch
  convert a character to numeric and back Skaperen 2 153 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  Type hinting - return type based on parameter micseydel 2 123 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Regex won't replace character with line break Tomf96 2 140 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  Need suggestion how to handle this error farrukh 1 128 Dec-21-2019, 03:21 PM
Last Post: DeaD_EyE
  How to get the index of a character from a string chihaya 1 151 Dec-03-2019, 12:54 PM
Last Post: buran
  Character Encodings Evil_Patrick 1 153 Nov-17-2019, 02:23 PM
Last Post: snippsat
  Substitution with regular expression returns hidden character SOH bajacri 2 217 Nov-17-2019, 03:38 AM
Last Post: bajacri

Forum Jump:


Users browsing this thread: 1 Guest(s)