Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please Use the provided code ## caesar.py def encode ( msg , shift ) : newmsg = #process each character in msg .
Please Use the provided code
## caesar.py
def encodemsg shift:
newmsg
#process each character in msg
#Hint: using a for loop
#A for loop to check each ch in msg:
if A ch and ch Z: #if the character is an uppercase letter
#encode the character based on the shift number
#Hint: the new character newch should be
#chrordch ordA shift ordA
elif a ch and ch z: #if the character is a lowercase letter
#Hint: Do similar thing to the above transformation
else: #if the character is not alphabetic
#Hint: What does the assignment ask you to do with such character?
#After the character is transformed, not put it as part of the new message
#Hint: which variables hold the new message and new character?
return newmsg
msg inputEnter message to be encrypted:
shiftstr inputEnter shift amount :
shift intshiftstr
printEncrypted message: encodemsg shift
You need to define a function encodemsg shift that has two parameters: a message
to be encoded and a shift number
In the function definition, you can use a for loop to enumerate every character in the
original message.
for ch in msg:
#check ch and modify it if needed
How to code a letter with a shift number?
When encoding a message, each upperlower case letter is coded into another
upperlower case letter based on the shift number. For example, if the shift number
is
letter H is coded into K letter o is coded into r letter z is coded into c
wraparound problem
Two functions ord and chr are useful for the coding. The ord function generates
the ASCII integer value of a character. For example, typing the following in Python
interpreter,
orda
ordA
The inverse to the ord function is the chr function. If you try the following in the
Python shell:
chr
A
chr
D
chra
To handle the wraparound problem for an upper letter, use the expression
chrordch ordA shift ordA
to calculate the encrypted version of an uppercase letter, where ch stores the letter
and shift stores the shift number. You will need a similar expression for the lowercase letters. For example, if ch holds letter B
ch B
shift
printch
B
ch chrordch ordA shift ordA
printch
E
Lets see another example: if ch holds letter Z
ch Z
ch chrordch ordA shift ordA
printch
C
Note that a string object in Python is immutable. When you encrypt the input
message, you can create a new encrypted message by
#at the beginning newmsg
#later while looping over each ch on the old message, you can
#make new message by attaching the new character encoded
# with the onprogress new message newmsg
newmsg newch
where ch holds the encoded character.
You can use input to read a string from standard input default device connecting
to keyboardYou can use raw input if your Python is older version. For example,
typing the following in Python interpreter:
msg inputEnter message to be encrypted:
Enter message to be encrypted: hello world
printmsg hello
world
You can read a shift number string from standard input default device connectingto
keyboard and transform the string to a number. For example, you may reference the
functions in the following sequence:
shiftstr inputEnter shift amount :
Enter shift amount :
shift intshiftstr
printshift
##
caesar.py
def encodemsg shift:
newmsg n
#process each character in msg
#Hint: using a for loop
#A for loop to check each ch in msg:
#encode the character based on the shift number
#Hint: the new character newch should be
#chr ordshiftord
#Hint: Do similar thing to the above transformation
else: #if the character is not alphabetic
#Hint: What does the assignment ask you to do with such character?
#After the character is transformed, not put it as part of the new message
#Hint: which variables hold the new message and new character?
return newmsg
msg inputEnter message to be encrypted:
shiftstr inputEnter shift amount :
shift intshiftstr
printEncrypted message: encodemsg shift
If you run your program with input as in the below examples, your program output should be same as illustrated.
RESTART
Enter message to be encrypted: Hello World
Enter shift amount :
Encrypted message: Khoor Zruog
RESTART
Enter message to be encrypted: Khoor
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started