Answered step by step
Verified Expert Solution
Link Copied!

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 encode(msg, 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
#chr((ord(ch)- ord('A')+ shift)%26+ ord('A'))
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 = input("Enter message to be encrypted: ")
shiftstr = input("Enter shift amount (1-25): ")
shift = int(shiftstr)
print("Encrypted message: "+ encode(msg, shift))
1. You need to define a function encode(msg, shift) that has two parameters: a message
to be encoded and a shift number
2. 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
3. How to code a letter with a shift number?
When encoding a message, each upper/lower case letter is coded into another
upper/lower case letter based on the shift number. For example, if the shift number
is
3, letter H is coded into K, letter o is coded into r, letter z is coded into c
(wrap-around 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,
>>> ord('a')97
>>> ord('A')65
The inverse to the ord() function is the chr() function. If you try the following in the
Python shell:
>>> chr(65)
'A'
>>> chr(65+3)
'D'
>>> chr(97)'a'
To handle the wrap-around problem for an upper letter, use the expression
chr((ord(ch)- ord('A')+ shift)%26+ ord('A'))
3
to calculate the encrypted version of an upper-case 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 =3>>>
print(ch)
B
>>> ch = chr((ord(ch)- ord('A')+ shift)%26+ ord('A'))
>>> print(ch)
E
Lets see another example: if ch holds letter Z,
>>> ch ='Z'
>>> ch = chr((ord(ch)- ord('A')+ shift)%26+ ord('A'))
>>> print(ch)
'C'
>>>
4. 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 on-progress new message newmsg =
newmsg + newch
where ch holds the encoded character.
5. You can use input() to read a string from standard input (default device connecting
to keyboard).(You can use raw input() if your Python is older version.) For example,
typing the following in Python interpreter:
>>> msg = input("Enter message to be encrypted: ")
Enter message to be encrypted: hello world
>>> print(msg) hello
world
6. 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 = input("Enter shift amount (1-25):")
Enter shift amount (1-25):23
>>> shift = int(shiftstr)
4
>>> print(shift)
23
##
caesar.py
def encode(msg, 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 ((ord(ch)-ord('A')+shift)%26+ord('A'))
#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 = input("Enter message to be encrypted: ")
shiftstr = input("Enter shift amount (1-25): ")
shift = int(shiftstr)
print("Encrypted message: "+ encode(msg, 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 (1-25): 3
Encrypted message: Khoor Zruog
>>>================================ RESTART ================================
>>>
Enter message to be encrypted: Khoor
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

What types of problems?

Answered: 1 week ago