Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python function called code_char(c, key) that takes a letter and a digit

ID: 3861461 • Letter: W

Question

Write a Python function called code_char(c, key) that takes a letter and a digit as input, and returns the letter shifted key positions to the right in the alphabet. The function should work with standard letters (uppercase or lowercases). You can assume that parameter c is always a letter from the alphabet.

Example: >>> code_char(‘a’, 2)

‘c’ >>>

code_char(‘U’, 6)

‘A’

You may need to use the following Python built-in function:

• chr that given an ASCII code returns the corresponding character

>>> chr(90)

'Z'

• ord given a character returns its ASCII code

>>> ord('j')

106

Explanation / Answer

def shift_n_letters(letter, n):
if ((ord(letter)>=97) and (ord(letter)<=122)):
result = ( ord(letter) + n - ord('a')) % 26
return chr( ord('a') + result )
elif ((ord(letter)>=65) and (ord(letter)<=90)):
result = ( ord(letter) + n - ord('A') ) % 26
return chr( ord('A') + result )
else:
print("Invalid input")

  
while True:
letter = input('Enter an alphabet:')
if len(letter) == 1:
break
print ('Please enter only one character:')
  
n = input("Enter a digit:")

print (shift_n_letters(letter, int(n)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote