Question: Python Program Help The Assignment: Assignment: Part 1 (30 points): Write the code to generate a key. This requires using a random number generator. Youll

Python Program Help

The Assignment:

Assignment: Part 1 (30 points): Write the code to generate a key. This requires using a random number generator. Youll need to import random at the top of your code. Then, in your code to get a random number, you would need to do the following: random.seed() num = random.randint(0,25) The first line forces the random number generator to actually randomize. The second line will pick a random number between 0 and 25 storing it in num. You dont want repeats so you will have to think about how you will choose letters from the original alphabet and concatenate them to the key string. Part 2 (40 points): Ask the user to enter a message, encrypt the message based on the key you used, and print the encrypted message to the screen. Part 3 (30 points): Ask the user to enter an encrypted message and a key. Write the code to decrypt the message based on the key the user provided.

My current answer:

import random

def keygen():

key = [] l

etters = "abcdefghijklmnopqrstuvwxyz"

assert len(set(letters)) == 26

random.seed()

for i in range(26):

num = random.randint(0, 25)

key.append(letters[num])

letters = letters[:num] + letters[num+1:]

return ''.join(key)

def enc(msg, key):

enc = ""

for letter in msg:

num = ord(letter.lower())

enc += key[num]

return enc

def dec(msg, key):

dec = ""

for letter in msg:

num = key.num(letter.lower())

dec += chr(num)

return dec

random.seed()

key = keygen()

print("Generated key:", key)

msg = input("Enter a message to encrypt: ")

enc += key[num]

num = random.randint(0, 25)

enc_msg = enc(msg, key)

print("Encrypted message:", enc_msg)

enc_msg = input("Enter an encrypted message: ")

key = input("Enter the key: ")

dec += chr(num)

num = random.randint(0, 25)

dec_msg = decrypt(enc_msg, key)

print("Decrypted message:", dec_msg)

The error coming up:

IndexError Traceback (most recent call last) /var/folders/1p/h9440rkj0xncydn6sgj_65rc0000gn/T/ipykernel_7905/3682687132.py in 28 random.seed() 29 ---> 30 key = keygen() 31 print("Generated key:", key) 32 /var/folders/1p/h9440rkj0xncydn6sgj_65rc0000gn/T/ipykernel_7905/3682687132.py in keygen() 8 for i in range(26): 9 num = random.randint(0, 25) ---> 10 key.append(letters[num]) 11 letters = letters[:num] + letters[num+1:] 12 return ''.join(key) IndexError: string index out of range

How do I fix this please help??

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!