-3

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)
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
Ada
  • 9
  • 1
  • 3
  • Dont call a variable `input` - it shadows the built in function – Patrick Artner Feb 06 '22 at 12:37
  • 2
    SO is no site for sharing your code and tasks. Do you have a question? [edit] and make clear what your problem is. [ask] and [tour] is a good place to start if you are new to this site. – Patrick Artner Feb 06 '22 at 12:38
  • `if ord('z'):` is always true as ord("Z") != 0 so its truthy. You need to compare stuff. `if character == "Z": character = "A"` would be one way to do it. – Patrick Artner Feb 06 '22 at 12:39
  • Thank you so much! The exercise emphasised that we should use ASCII. Could you rewrite the code using ascii numbers. Sorry for all hassle – Ada Feb 06 '22 at 17:32

3 Answers3

1

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 cases...


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')
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
1

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)
Ada
  • 9
  • 1
  • 3
0

I have a very simple answer:

myString = input()

name = ord(myString)

gor = name + 1

if name == 90:
   gor = 65
   

print(chr(gor))
toyota Supra
  • 3,181
  • 4
  • 15
  • 19