# Using 20 numbers in random order to randomize the characters in random order
key = [16, 2, 9, 20, 8, 12, 14, 10, 5, 19, 18, 15, 1, 11, 7, 3, 17, 13, 6, 4]
# Initializing a list to do encryption with
encryption = [""] * 20
# Initializing a list to do decryption with
decryption = [""] * 20
# Starts a loop that asks the user to input 1, 2, or 3 in our menu. Will loop until appropriate input is given.
while True:
menu_option = input("\n1) Encoding\n2) Decoding\n3) Exit\nPlease make a selection -> ")
# Validates that menu input is a number between 1-3
try:
menu_option = int(menu_option)
if menu_option < 1 or menu_option > 3:
print("Please input a number between 1-3.")
except:
print("Please input a number between 1-3.")
# Encoding Section
if menu_option == 1:
while True:
# Asks the user for something to encode
plaintext_msg = input("\nPlease enter a message to encode -> ")
# Raises an error if the input is above 20 characters, and loops until appropriate input is given
if len(plaintext_msg) > 20:
print("Message cannot be more than 20 characters.")
continue
# Turns our string into a list of individual characters
plaintext_msg = list(plaintext_msg)
# Iterates for the amount of characters in our user input
for i in range(len(plaintext_msg)):
# Places characters in the "encryption" list.
encryption[key[i]] = plaintext_msg[i]
# Turning our list into a string variable
encryption_as_str = encryption
# Printing the encoded message, breaking the loop and reprompting the user
print("Your encoded message is: " + "".join(encryption_as_str))
break```
#๐ list assignment index out of range
62 messages ยท Page 1 of 1 (latest)
@mellow ravine
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
so, the issue is with this line:
encryption[key[i]] = plaintext_msg[i]
oops the indentation
so, i understand that it would be fixed by changing it to i -1, but i don't understand why?
if an index starts at 0.. how is that possible?
Can you show the full exception and traceback?
The index error is not from i, it's from key[i], whose values range from from 1..20, not 0..19.
oh !!!
Detailed print()s would show you this. Before the line where it explodes, print(i), print(key[i]) and so on. In separate prints so that when one fails the previous ones have succeeded, providing you information.
i've never tried that but that's a really good idea
hang on let me try to make sense of this for a sec
The error actually underlines the key[i] part, which is super helpful, and new in python 3.12 or 3.13, something very recent.
That printout will be more helpful with print("i", i), print("key[i]", key[i]) and so on ๐
okay thanks a lot for your help... i may leave this thread open for a bit just in case i have any more issues while i finish up this assignment but i'm glad i understand it haha. someone in the discord helped me fix that line but i didn't fully get what happened
unsure of how to map these back into the list
like, i would have to take the 16th letter in the encoded string and put it first, and so on, according to the key
if you have a rough idea i'll try my best :)
You could use key[i]-1, knowing the range is 1..20. Or you could use key[i]%20, but I suspect such agnosticism might lead to not being able to reverse things later.
sorry i meant for the decoding portion haha
the encoding seems to be working great
Maybe you'd better be more specific then.
well, the way the encoding works is basically taking the first letter and putting it in the 16th index in a new list, etc, up to 20 places
Ah, so you're just shuffling the plaintext letters around.
yeah !
i'm in intro to programming, it's not really "encoding" as much as a basic cipher lol
Still an encoding.
ok cool ๐ just makes it sound fancier than it is
i think the main issue
for decoding, i would have to take the 16th letter and put it first, right? what if there is no 16th letter? i'm not sure how to pass it
Run the for i loop just like the encode process, but instead of:
encoding[key[i]] = plaintext[i]
go:
plaintext[i] = encoding[key[i]]
Iterate over the plaintext length as before. You'll be reaching further than that into encoding, but that's ok, because that's where the text went in the first place.
You're just swapping the assignment around.
After all, after the encoding assigning these two places were equal. You started with plaintext and make it so for the relevant parts of the encryption. Now you're starting with the relevant parts of the encryption and making it so for the plaintext.
hmm, i'm getting out of range again. let me do the print thing and play with it
what on earth did i do lol
i do have it in the list now tho ๐ i think i just have to mess with it and i can probably figure it out
it's cool that you spend so much time helpin dumb ass first year kids like me i appreciate you brother
You printed some_list.sort instead of some_list.sort()
which, btw, returns None because .sort sorts the list in place rather than returning a new list with sorted values
Did you shift to using key[i]-1 in the encryption end? You need that same change in the decryption.
i can show you the full code one sec
i'm trying to figure out a more elegant way to do...... this. lol
for i in range(len(key)):
final_decryption[i] = decryption[key[i]]
You probably need to key[i]-1 because its range is 1..20.
You get this by asking: "how did I fill in the index in decryption[9]" ? How is 9 obtained?
omg that worked thank youuu
i was trying to do it all in the same for loop and it was notttt working for me lmao
it is officially functional ๐ there are some things i want to clean up but im happy
once again thanks a lot
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.