This coding exercise is giving me a lot of trouble and I need help.
Here is the problem.
Write a program which does the reverse of the example above: it should take a character as input and output the corresponding number (between 1 and 26). Your program should only accept capital letters. As error-checking, print invalid if the input is not a capital letter.
Here is what I have so far.
inp = input()
if (len(inp) > 1 or inp != inp.upper()):
print("invalid input")
else:
print(ord(inp)-ord("A")+1)
As clarified in the comment, the problem appears to be that the validation doesn't cover enough cases.
To check that the number you calculate is a valid upper-case character you could just check if it is between 1 and 26:
if 1 < ord(inp) - ord("A") + 1 < 26:
print(ord(inp) - ord("A") + 1)
else:
print("Invalid input")
To add this to your current validation:
inp = input()
if (len(inp) > 1 or inp != inp.upper() or
ord(inp) - ord("A") + 1 < 1 or ord(inp) - ord("A") + 1 > 26):
print("invalid input")
else:
print(ord(inp)-ord("A")+1)