0

I have been coding with Python for a few months, and I have already coded a successful cipher, but I am making a random cipher and it won't work. I have attached the code.

    txt=input("Input message.\n")
  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,25,26]
  ra.shuffle(numbers)
  a1=numbers[1]
  b1=numbers[2]
  c1=numbers[3]
  d1=numbers[4]
  e1=numbers[5]
  f1=numbers[6]
  g1=numbers[7]
  h1=numbers[8]
  i1=numbers[9]
  j1=numbers[10]
  k1=numbers[11]
  l1=numbers[12]
  m1=numbers[13]
  n1=numbers[14]
  o1=numbers[15]
  p1=numbers[16]
  q1=numbers[17]
  r1=numbers[18]
  s1=numbers[19]
  t1=numbers[20]
  u1=numbers[21]
  v1=numbers[22]
  w1=numbers[23]
  x1=numbers[24]
  y1=numbers[25]
  z1=numbers[26]
  alphabet=['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']
  a=alphabet[a1]
  b=alphabet[b1]
  c=alphabet[c1]
  d=alphabet[d1]
  e=alphabet[e1]
  f=alphabet[f1]
  g=alphabet[g1]
  h=alphabet[h1]
  i=alphabet[i1]
  j=alphabet[j1]
  k=alphabet[k1]
  l=alphabet[l1]
  m=alphabet[m1]
  n=alphabet[n1]
  o=alphabet[o1]
  p=alphabet[p1]
  q=alphabet[q1]
  r=alphabet[r1]
  s=alphabet[s1]
  t=alphabet[t1]
  u=alphabet[u1]
  v=alphabet[v1]
  w=alphabet[w1]
  x=alphabet[x1]
  y=alphabet[y1]
  z=alphabet[z1]
  txta=txt.replace("a","á")
  txtb=txta.replace("b","+")
  txtc=txtb.replace("c","ç")
  txtd=txtc.replace("d","ð")
  txte=txtd.replace("e","é")
  txtf=txte.replace("f","đ")
  txtg=txtf.replace("g","ŋ")
  txth=txtg.replace("h","ħ")
  txti=txth.replace("i","í")
  txtj=txti.replace("j","j̉")
  txtk=txtj.replace("k","ĸ")
  txtl=txtk.replace("l","ł")
  txtm=txtl.replace("m","µ")
  txtn=txtm.replace("n","n̉")
  txto=txtn.replace("o","ó")
  txtp=txto.replace("p","þ") 
  txtq=txtp.replace("q","@")
  txtr=txtq.replace("r","¶")
  txts=txtr.replace("s","ß")
  txtt=txts.replace("t","ŧ")
  txtu=txtt.replace("u","ú")
  txtv=txtu.replace("v","̉")
  txtw=txtv.replace("w","ẃ")
  txtx=txtw.replace("x","»")
  txty=txtx.replace("y","ý")
  txtz=txty.replace("z","«")
  texta=txtz.replace("á",a)
  textb=texta.replace("+",b)
  textc=textb.replace("ç",c)
  textd=textc.replace("ð",d)
  texte=textd.replace("é",e)
  textf=texte.replace("đ",f)
  textg=textf.replace("ŋ",g)
  texth=textg.replace("ħ",h)
  texti=texth.replace("í",i)
  textj=texti.replace("j̉",j)
  textk=textj.replace("ĸ",k)
  textl=textk.replace("ł",l)
  textm=textl.replace("µ",m)
  textn=textm.replace("n̉",n)
  texto=textn.replace("ó",o)
  textp=texto.replace("þ",p)
  textq=textp.replace("@",q)
  textr=textq.replace("¶",r)
  texts=textr.replace("ß",s)
  textt=texts.replace("ŧ",t)
  textu=textt.replace("ú",u)
  textv=textu.replace("̉",v)
  textw=textv.replace("ẃ",w)
  textx=textw.replace("»",x)
  texty=textx.replace("ý",y)
  text=texty.replace("«",z)
  print(text)

It just comes up with IndexError: list index out of range. Anybody got any idea why? It affects the letter z, and when I remove it, it affects the letter j. And if you're wondering why there are special characters, it's so that if a goes to z and z goes to a then a will go to z which will then go back to a. I call it double substitution.

CDJB
  • 14,043
  • 5
  • 29
  • 55
OnlyTrueJames
  • 47
  • 1
  • 8
  • Instead of using `numbers` you can shuffle `alphabet`, you also don't need to do it manually `alphabet = list(string.ascii_lowercase) random.shuffle(alphabet) txt.replace("á", alphabet[0]).replace("+", alphabet[1])` – Guy Nov 18 '19 at 09:34
  • There are so many things wrong on so many levels. Might I suggest you take a look at loops? And collections? – SimonC Nov 18 '19 at 09:38
  • I am new compared to you. I will change it, I just tried to use it from a .txt and used a number, then gave up on the file, and the code is left over. I might amend it. https://repl.it/@GnomeDev/TotalShockingMacrolanguage-2nd-cipher is the code. – OnlyTrueJames Nov 18 '19 at 14:09

2 Answers2

2

Lists in python are zero-based. For example, numbers[1] in your code would give 2 - the second value in the list.

To fix this in your code, remove 26 from your list indices. You should start indexing at 0.

Nils
  • 910
  • 8
  • 30
CDJB
  • 14,043
  • 5
  • 29
  • 55
1

This code has many things wrong with it, but it looks like you have the basic idea down. What I would recommend trying first is researching for, while, if and def.

An easy thing to practice with is see how many lines you can cram a rock-paper-scissors program into. The lower the better, because this means you are using more complex functions.

You don't need to create a list for the alphabet, you can use ord(letter) to get the ascii form of the letter, so:

if ord(letter) == ord(a) :

Making it a bit easier than creating a whole alphabet, when there is already one present.

And if you want to randomly shuffle 26 numbers, you can do a for loop:

for i in range(97, 123) : #Because it counts the 0th first and 97 in ascii is a, 122 is z

     list.append(chr(i))

ra.shuffle(list)


# Another fix to make this a bit easier is to use a for loop as your encryption.
for i in range(0, len(list input)) : # will loop by the amount they input so it can technically go forever
    if ord(listInput[i]) == ord(list[i]) :
         listInput[i] = 'á' # and so forth

list[i] being a in the list of randomly shuffled letters

Kevin
  • 16,549
  • 8
  • 60
  • 74
CanadianCaleb
  • 136
  • 10