Question

Can someone help me with this? ## caesar.py def encode(msg, shift):    newmsg = ""   ...

Can someone help me with this?

## 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))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def encode(msg, shift):
newmsg = ""
  
#process each character in msg.
#Hint: using a for loop
for ch in msg:
#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
newmsg+=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
newmsg+=chr((ord(ch) - ord('a') + shift) % 26 + ord('a'))
else: #if the character is not alphabetic
#Hint: What does the assignment ask you to do with such character?
newmsg+=ch
#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))

output:

Add a comment
Know the answer?
Add Answer to:
Can someone help me with this? ## caesar.py def encode(msg, shift):    newmsg = ""   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Python script file called hw.py. Then import the module random: from random import *...

    Create a Python script file called hw.py. Then import the module random: from random import * Thanks in advance! Ex. 1. Write a function called cleanLowerWord that receives a string as a paramcter and retums a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this,...

  • Python program Use the provided shift function to create a caesar cipher program. Your program s...

    python program Use the provided shift function to create a caesar cipher program. Your program should have a menu to offer the following options: Read a file as current message Save current message Type in a new message Display current message "Encrypt" message Change the shift value For more details, see the comments in the provided code. NO GLOBAL VARIABLES! Complete the program found in assignment.py. You may not change any provided code. You may only complete the sections labeled:#YOUR...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Good evening, I've been toiling around with restructuring a program which is supposed to register a...

    Good evening, I've been toiling around with restructuring a program which is supposed to register a preexisting input file, which contains four different passwords, and have it pass through the verification process, to see which of those passwords meets all the criteria set by the highlighted parameters. The code stipulates that in order for a password to be considered valid, it should contain a minimum of 6 characters in total, at least 2 lowercase letter, at least 1 uppercase letter,...

  • Do this using the C language. show me the code being executed and also copy and...

    Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • CAN ANYONE PLEASE HELP ME SOLVE THIS PROBLEM WITHOUT RETURNING ERRORS, BAD TOKENS, ETC. PLEASE INCLUDE...

    CAN ANYONE PLEASE HELP ME SOLVE THIS PROBLEM WITHOUT RETURNING ERRORS, BAD TOKENS, ETC. PLEASE INCLUDE CORRECT SYNTAX...MUST BE IN PYTHON 3 AND PASS THE TEST Write a function analyze_text that receives a string as input. Your function should count the number of alphabetic characters (a through z, or A through Z) in the text and also keep track of how many are the letter 'e' (upper or lowercase). Your function should return an analysis of the text in the...

  • I have a python project that requires me to make a password saver. The major part...

    I have a python project that requires me to make a password saver. The major part of the code is already giving. I am having trouble executing option 2 and 3. Some guidance will be appreciated. Below is the code giving to me. import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT