python For this part you will be writing a series of functio

python

For this part you will be writing a series of functions that can be used as part of a \"secret message encoder\" program. Here are the functions you will be writing as well as some sample code that you use use to test your work. # function : addletters # input: a word to scramble (String) and a number of letters (integer) # processing: adds a number of random letters (A-Z; a-z) after each letter - in the supplied word. for example, if word-\"cat\" and num-1 we could generate any of the following: cZaQtR cWaRts cEaett if word-\"cat\" and num-2 we could generate any of the following: cRtaHFtui cnnaNYtjn czAaAitym # output: returns the newly generated wo rd def add_letters(word, num): # function code goes here!

Solution

import random def add_letters(word,num): scrambledword=\'\' for character in word: scrambledword+=character for number in range(num): randletter=random.randint(65,122) while randletter>90 and randletter<97: randletter=random.randint(65,122) scrambledword+=chr(randletter) return scrambledword def remove_letters(word,num): new_word=word[0::num+1] return new_word def shift_characters(word,num): newword=\'\' for character in word: character=ord(character)+num newword+=chr(character) return newword def main(): function=input(\"(e)ncode, (d)ecode, or (q)uit: \").lower() while function!=\'q\': if function==\'e\': number=int(input(\"Enter a number between 1 and 5: \")) while number<1 or number>5: print(\"That is not a valid number\") number=int(input(\"Enter a number between 1 and 5: \")) phrase=input(\"Enter a phrase to encode: \") encodedword=add_letters(phrase,number) final_word=shift_characters(encodedword,number) print(\"Your encoded word is: \",final_word) function=input(\"(e)ncode, (d)ecode, or (q)uit: \") elif function==\'d\': number=int(input(\"Enter a number between 1 and 5: \")) while number<1 or number>5: print(\"That is not a valid number\") number=int(input(\"Enter a number between 1 and 5: \")) phrase=input(\"Enter a phrase to decode: \") encodedword=remove_letters(phrase,number) final_word=shift_characters(encodedword,number*-1) print(\"Your decoded word is: \",final_word) function=input(\"(e)ncode, (d)ecode, or (q)uit: \") main()
python For this part you will be writing a series of functions that can be used as part of a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site