Another modify task, and this one works with ASCII codes.
A game hides its messages by shifting every character exactly 3 places
forward in ASCII, so that A becomes D. To decode a character, the program
has to turn it into its ASCII code, add 3, and turn the result back into a
character.
The programmer has started the code, but left gaps where the conversions between strings and ASCII codes should go. Every gap is marked with question marks:
print("Enter character:")
char_input = input()
ascii_val = ???char_input
new_val = ascii_val ??????
new_char = ???new_val
print("Shifted character is: " + ????)
The editor starts you off with this code. It does not run as it stands: Python stops at the first line of question marks.
Modify the program so that it:
Shifted character is: .Keep the question exactly as it is. The user always types exactly one character.
The question first, worded exactly like this:
Enter character:
Then one line, where CHARACTER is the character three places further along
in ASCII:
Shifted character is: CHARACTER
Input:
A
Output:
Enter character:
Shifted character is: D
Input:
x
Output:
Enter character:
Shifted character is: {
The shift follows the ASCII codes and does not start again at a after z, so
a letter near the end of the alphabet turns into whatever comes three codes
further on. Here that is {, the character with code 123. Digits and
punctuation shift too: 7 becomes :.
Make sure that you check for the following things:
ord turns a character into its code, and chr turns a code back into a
character. Check you have not swapped them.ord and chr put their brackets around what they are given, as in
ord(char_input).print outputs the new character, not the new code.