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
Question [subprocess] Simple way to handle error in Popen()? Winfried 1 685 Oct-14-2025, 09:22 AM
Last Post: snippsat
  is any character in s1 in s2 Skaperen 6 2,123 Sep-07-2024, 12:18 AM
Last Post: Skaperen
  Input error correction. shakoun 4 2,382 Jun-07-2024, 12:59 PM
Last Post: menator01
  python escaping character atom509 1 1,264 May-29-2024, 08:57 PM
Last Post: DeaD_EyE
  Python rule about the space character surrounding the equal sign ineuw 10 6,984 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 8,188 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Wrong type error rowan_bradley 6 3,749 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 7,194 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 12,702 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  use of escape character in re.sub and find WJSwan 1 2,051 Feb-16-2023, 05:19 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.