233k views
2 votes
In the last problem, we get an error if we enter more than one character at a time. Rewrite the program so that it only calls the ord method if a single character is entered.

I have this code so far I just don't know how to make it print not a character
ch= input("Enter a character: ")
print("ASCII #%d" %ord(ch[0]))

1 Answer

2 votes

Answer:

ch= str(input("Enter a character: "))

if str.isalpha(ch)==True:

print("ASCII #%d" %ord(ch))

else:

print("Not accepted")

Step-by-step explanation:

Above, we check if the input is an alphabet, and only then ord is called or else Not accepted message is printed.

User Matt Leach
by
5.1k points