Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Encryption, List Ranges, and Python...
-
Here is my module:
char_dict = ["a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","?","!",".",",","_"," "] def encrypt(message,key): end_msg = "" through = 0 for char in message: end_msg = end_msg + char_dict[char_dict.index(char) + key] through = through + 1 return end_msg
Here is the file that uses the module:
import complexx print(complexx.encrypt("zebra",15))
My problem is that when I use the key to offset the character to a new one by adding the index, and getting the resulting index and using it's string from the list, if you go too far (like demonstrated in the example code, you can try it out) it will say "string index out of range" because I am using a key that hops too many spaces ahead, and ends up going out of bounds (or indexes) of the list. I need
-
...to know how to be able to make the loop (if the index hits the end of the dictionary), start back at the beginning. An example:
If I had the string I wanted to encrypt which was "z", and I was using the key "8", I would technically be going out of range by 1 index, but I want it to return the value of "a" (because I want it to loop back to the beginning). Is any of this making sense?
Sum-up: If the list index is out of range, start the index back at the beginning.
-
To resolve the string index out of range could you not do something like:
char_list = ["a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","?","!",".",",","_"," "] def encrypt(message,key): end_msg = "" through = 0 for char in message: size = (char_list.index(char) + key) % len(char_list) end_msg = end_msg + char_list[size] through = through + 1 return end_msg
Also for purposes of naming it should be
char_list
notchar_dict
due to the fact that you have used a list not a dict. -
You can also generate you char_list if you want. I wanted to try for fun, it was a little more convoluted than I thought it would be. But I learnt something by trying. Maybe it can be expressed a lot easier than this. But ok, this is what I did.
extra_chars = ["?","!",".",",","_"," "] # 2 steps x = [(chr(ind + 32), chr(ind)) for ind in range(65,91)] y = [tp for tupl in x for tp in tupl] char_list1 = y + extra_chars print char_list1, '\n' # 1 step char_list2 = [tp for tupl in [(chr(ind + 32), chr(ind)) for ind in range(65,91)] for tp in tupl] + extra_chars print char_list2
-
Use the Force Luke! == Use the Standard Library @Phuket2 ...
import string char_list2 = [] for i in xrange(len(string.ascii_lowercase)): char_list2.append(string.ascii_lowercase[i]) char_list2.append(string.ascii_uppercase[i]) print(char_list2) import itertools char_list3 = list(itertools.chain(*zip(string.ascii_lowercase, string.ascii_uppercase))) print(char_list3)
-
@ccc , LOL, I noticed you did an edit 😋
I was also using string module in the first place. But because in the end my solution didn't need it, I didn't use it. But yes, I am a little list comp happy at the moment. But I really wanted to solve it using a list comp. I tried all sorts of crazy things with zip and inverse zip (*). But I had to find the answer on stackflow. But still comfortable enough with list comps now to combine the outer answer with my inner answer to the problem.I have never used itertools. That will come.
But I hope we have not overwhelmed @AtomBombed.but @AtomBombed , is a great place to learn here because of the various inputs from people. I am still new to Python, but the guys her have taught me so much, in the right way. I hope you get something from our comments.
Oh, @ccc I was also using xrange vrs range. I think on this small list, either would be OK. Larger ranges, for sure you xrange. @AtomBombed , also good reading for you. This case we are getting 32 ints (overhead excluded), but if the range was range(1, 1,000,000) for example, range would make a list of all the numbers and return it. A lot of memory and time. xrange on the hand is know as a generator. It will return each number as required. A very small memory foot print. Maybe you are thinking too much info for you at the moment. But really, the early you learn about this stuff the better.
I hope I was not a little wrong in my explanation above otherwise I am sure I will hear about it 😁😛 but it will be in a constructive way