Write a program that takes a character as input (a string of length 1), which you should assume is an upper-case character; the output should be the next character in the alphabet.
If the input is 'Z'
, your output should be 'A'
.
Here's what i've done so far but I’m not getting A
when I input Z
. I’m getting [
.
Please help, what am I doing wrong?
input = (input()).upper()
for character in input:
number = ord(character) 1
number1 = chr(number)
if ord('z'):
new_number = ord(character) - 25
number2 = chr(new_number)
print(number1)
CodePudding user response:
A way of doing this may be via a match
statement:
match (letter):
case 'A':
print('B')
case 'B':
print('C')
But you will need around 30 case
s...
A better idea is to use a list
:
letters = ['A', 'B', 'C'...]
then to get the letter
letter = input().upper()
and then to get the next element in the list
:
print(letters[letters.find(letter) 1])
but there will be an error raised for 'Z'
, so you will need a try
/except
block for IndexError
:
try:
print(letters[letters.find(letter) 1])
except IndexError:
print('A')
CodePudding user response:
I have figured it out!! Thanks to everyone for reaching out with an alternative method! I'm ever grateful.
input = (input()).upper()
encrypted = ""
for character in input:
if character == "":
encrypted = ""
elif ord(character) 1 > ord("Z"):
encrypted = chr(ord(character) 1 - 26)
else:
encrypted = chr(ord(character) 1)
print(encrypted)