0

I'm creating a program that makes a random code list and translates the text to the code. I'm having trouble figuring out how to make it so that the code is equal to a single random letter in a list.

This is the code I have so far :

loop = 0
while loop < 24:
    numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
    letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    random.shuffle(numbers)
    random.shuffle(letters)
    print(numbers, "=", letters)
    loop = loop + 1
    if loop == 24:
        print("test")

It's outputting something like this:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

It should, in theory, look something like this when it's outputted: 1 = g, 2 = w, 3 = k, and so on.

  • 1
    This isn't at all clear. Your code doesn't even attempt to use the `random` module. Maybe you could look into thinks like `random.shuffle` and `random.sample`. – John Coleman Apr 30 '19 at 18:09
  • Yeah, I just realized that I was trying something else with the code and forgot to change that back. I'll change that now –  Apr 30 '19 at 18:12
  • store the random number generated in a set and check for the new random number. If you are able to add new random number in the set, then it is not used, else it is already assigned. – vb_rises Apr 30 '19 at 18:12
  • You just need the ```letters``` and then ```shufle()``` it. You will have a random index/letter association already. – accdias Apr 30 '19 at 18:19
  • okay thanks Vishal I'll implement those. Do you know how to select one random number and letter for it? –  Apr 30 '19 at 18:20
  • Just noticed you are doing ```loop=0``` and ```while loop < 24:``` which will count from 0 to 23, and it looks like it is not what you want. You better replace that (and delete the associated ```loop = loop + 1``` bit) with ```for loop in range(26):```. – accdias Apr 30 '19 at 18:54

2 Answers2

0

Here is a example of what I said in my comment:

from random import shuffle
from string import ascii_lowercase

letters = list(ascii_lowercase)
shuffle(letters)

list(enumerate(letters))

The output of the above code is something like this:

>>> from pprint import pprint
>>> pprint(list(enumerate(letters)))
[(0, 'z'),
 (1, 'a'),
 (2, 's'),
 (3, 'y'),
 (4, 'o'),
 (5, 'h'),
 (6, 'p'),
 (7, 'v'),
 (8, 'l'),
 (9, 'r'),
 (10, 'e'),
 (11, 'i'),
 (12, 'm'),
 (13, 'g'),
 (14, 'b'),
 (15, 'q'),
 (16, 'n'),
 (17, 'u'),
 (18, 'd'),
 (19, 'k'),
 (20, 'x'),
 (21, 'c'),
 (22, 'j'),
 (23, 't'),
 (24, 'f'),
 (25, 'w')]
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31
0

You can create a dictionary lookup for shuffled values to and from lower ascii using the string module and random - enumerate gives you indexes into the shuffled string:

from string import ascii_lowercase
import random
atoz = list(ascii_lowercase)
random.shuffle(atoz)

# add the index:character translation    
d = { idx:char for idx,char in enumerate(atoz) }

# update with the reverse translation:
d.update( {c:k for k,c in d.items() } )
print(d)

Output:

{0: 'w', 1: 'e', 2: 'v', 3: 'r', 4: 'm', 5: 'n', 6: 'z', 7: 'x', 8: 'g', 
 9: 'j', 10: 'p', 11: 'y', 12: 'q', 13: 'c', 14: 'u', 15: 'a', 16: 't', 
17: 'l', 18: 'f', 19: 's', 20: 'h', 21: 'i', 22: 'b', 23: 'o', 24: 'k', 
25: 'd', 
'w': 0, 'e': 1, 'v': 2, 'r': 3, 'm': 4, 'n': 5, 'z': 6, 'x': 7, 'g': 8, 
'j': 9, 'p': 10, 'y': 11, 'q': 12, 'c': 13, 'u': 14, 'a': 15, 't': 16, 
'l': 17, 'f': 18, 's': 19, 'h': 20, 'i': 21, 'b': 22, 'o': 23, 'k': 24, 'd': 25}

To translate using this dictionary:

def word_to_ints(word, d):
    return [d.get(c,c) for c in word.lower()]

def ints_to_word(ints, d):
    return ''.join( (d.get(n,n) for n in ints) )

k = word_to_ints("Translate me", d )

print(k, "=", ints_to_word(k, d))

Output:

[16, 3, 15, 5, 19, 17, 15, 16, 1, ' ', 4, 1] = translate me

Doku:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69