-1

I'm starting to write a program that make all the letters from the alphabet be transformed in symbols and "codified".

import random

char = ("@", "#", "$", "%", "&", "*", "-", "¿", "=", "?", "~", "§", "!", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "£", "º", "°")

a = random.choice(char)
b = random.choice(char)
c = random.choice(char)
d = random.choice(char)
e = random.choice(char)
f = random.choice(char)
g = random.choice(char)
h = random.choice(char)
i = random.choice(char)
j = random.choice(char)
k = random.choice(char)
l = random.choice(char)
m = random.choice(char)
n = random.choice(char)
o = random.choice(char)
p = random.choice(char)
q = random.choice(char)
r = random.choice(char)
s = random.choice(char)
t = random.choice(char)
u = random.choice(char)
v = random.choice(char)
w = random.choice(char)
x = random.choice(char)
y = random.choice(char)
z = random.choice(char)

print 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

How can I make every single letter have it's own symbol and not repeat? Some output I got:

°10?¿012=~===7251@&=2784£-

°¿~°°?¿£%!2~$270!5º%5@0*1º

~6@&~0£!896~!7?$0-£8461-¿-
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
shm
  • 29
  • 1
  • 7

1 Answers1

3

Use random.shuffle() and don't assign to 26 separate variables:

import random

char = list(u"@#$%&*-¿=?~§!1234567890£º°")
random.shuffle(char)

print(u''.join(char))

Here char is a (randomly shuffled) list, where the indices could be used instead of using separate variables. So a is found at index 0 in the list.

I made the characters a list of Unicode codepoints, since you are using non-ASCII characters here.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I'm using python 2.7.10 – shm Jan 16 '17 at 17:51
  • Traceback (most recent call last): File "python", line 6, in UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 0: invalid continuation byte – shm Jan 16 '17 at 17:57
  • @shm: right, that's because we can't just reshuffle UTF-8 bytes. I've made it a Unicode string instead, you are using non-ASCII characters, after all. – Martijn Pieters Jan 16 '17 at 17:59