Python 3.0=< Now you need to implement a function called code_block (word, key)
ID: 3862059 • Letter: P
Question
Python 3.0=<
Now you need to implement a function called code_block (word, key) that takes an 8-letter word and an 8-digit key as parameters and encrypt each letter of the word with the corresponding digit of the key. The function code_block must call the function code_char that you have implemented in question 1.
>>> code_block(’abcdefgh’,’12121212’)
’bddffhhj’
>>>code_block(’xxyyxxaa’,’44556677’)
'bbddddhh'
• You will need to use the string concatenation operator + for joining up strings.
>>> 'a'+'b' 'ab'
• The key used to encrypt the message is a string so you will need the builtin function int to convert an ASCII digit into its corresponding number
>>> int('4')
4
code char=
def code_char(c,key):
if ((ord(c)>=97) and (ord(c)<=122)):
result= (ord(c) + key - ord('a'))%26
return chr(ord('a') + result)
elif ((ord(c)>= 65) and (ord(c) <= 90)):
result= (ord(c) + key - ord('A'))%26
return chr(ord('A') + result)
else:
print ("invalid input")
Explanation / Answer
def code_block(word,key):
encrypted_word = ""#take zero character string initially
for (w,k) in zip(word,key):# iterate each character from word and key
encypted_word = encrypted_word + code_char(w,int(k))#calling and appeding each encrypted character
return encrypted_word#returning final encrypted string
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.