Cryptography Problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Cryptography Problem (/thread-1707.html) |
Cryptography Problem - PYTHONEUR - Jan-21-2017 Please Help me what's the prob with str and int If someone can explain this problem They said me : TypeError: Can't convert 'int' object to str implicitly CODE : Test the code in this super dupper website : http://www.pythontutor.com/visualize.html#mode=display Msg ="MATHS" def num (caractere): return ord (caractere)-65 def lettre (n): return chr (n+65) for c in Msg : print (c,end="|") print() for c in Msg : print(num(c), end=" | ") print ('\n\n', 'le message est sous forme de chiffres') for c in Msg: print ( '\n\n','Maintenant veuillez entrer deux chiffres pour créer votre cles ') # Finir Cle print ('\n\n', 'soit la fonction affine a*x + b qui va vous permettre de créer votre cles ') A = 11 B = 7 for c in Msg: r=chr((A*(ord(c)-65)+ B)%26 + 65) print(lettre(r), end=" | " ) RE: Cryptography Problem - Larz60+ - Jan-21-2017 Please use code tags in the future. I updated for you this time As far as the error is concerned, please include the full trace back, it has important information that is need for diagnosis RE: Cryptography Problem - PYTHONEUR - Jan-21-2017 Hello, thank you but what do you mean by full trace ? I haven't python, i use just the website Here the link here for test the code in (((((Python 3.3))))) : http://www.pythontutor.com/visualize.html#mode=display RE: Cryptography Problem - Larz60+ - Jan-21-2017 Quote:They said me : TypeError: Can't convert 'int' object to str implicitly Who is 'they'? Are you using on-line Python intreperter? The error usually has to do with concatenation, trying to insert an integer directly into a string example: mystring = 'Abcdef' + 5you need to convert the integer to a string first like: mystring = 'Abcdefg' + '5' RE: Cryptography Problem - PYTHONEUR - Jan-22-2017 Oh "they" I just told the developers while it's the program lol Yes i use python On-line, i don't have python because i have Ubuntu But excuse me i don't fully understand The prob is in return chr how i can use correctly int and str, i test the two and it's not work. return chr ('n'+'65') RE: Cryptography Problem - Larz60+ - Jan-22-2017 Ok, you need to get the character value 0r 65, which is ASCII 'A' so it's return 'n' + chr(65) # or you can use return 'n' + 'A'To get the numeric value of a character use ord('A') RE: Cryptography Problem - wavic - Jan-22-2017 (Jan-22-2017, 06:13 AM)Larz60+ Wrote: es i use python On-line, i don't have python because i have UbuntuYou are with Ubuntu? And yiu are saying that you don't have Python on it? Every Linux distribution comes with Python pre installed since the Linux system counts of 50% of Perl and Python scripts RE: Cryptography Problem - PYTHONEUR - Jan-22-2017 Hi Wavic Yes i know i have ubuntu in linux but my ubuntu/linux system are break like python :( So no Python scripts Thanks for the explanation I have a little better advanced, but now I encounter another problem how to convert correctly into letter? In the example below I use ord to convert to a letter Msg ="MATHS" def num (caractere): return ord (caractere)-65 def lettre (n): return n + ord(65) for c in Msg : print (c,end="|") print() for c in Msg : print(num(c), end=" | ") print ('\n\n', 'le message est sous forme de chiffres') for c in Msg: print ( '\n\n','Maintenant veuillez entrer deux chiffres pour créer votre cles ') # Finir Cle print ('\n\n', 'soit la fonction affine a*x + b qui va vous permettre de créer votre cles ') A = 11 B = 7 for c in Msg: r=(A*(ord(c)-65)+ B)%26 + 65 print(r, end=" | " ) for c in Msg: print(lettre(r), end=" | ") Thanks for the explanation I have a little better advanced, but now I encounter another problem how to convert correctly into letter? In the example below I use ord to convert to a letter Msg ="MATHS" def num (caractere): return ord (caractere)-65 def lettre (n): return n + ord(65) for c in Msg : print (c,end="|") print() for c in Msg : print(num(c), end=" | ") print ('\n\n', 'le message est sous forme de chiffres') for c in Msg: print ( '\n\n','Maintenant veuillez entrer deux chiffres pour créer votre cles ') # Finir Cle print ('\n\n', 'soit la fonction affine a*x + b qui va vous permettre de créer votre cles ') A = 11 B = 7 for c in Msg: r=(A*(ord(c)-65)+ B)%26 + 65 print(r, end=" | " ) for c in Msg: print(lettre(r), end=" | ") Now i have a new prob : TypeError: ord() expected string of length 1, but int found Forget all that I have said now, why i have that valor : "nA" Hi Wavic Yes i know i have ubuntu in linux but my ubuntu/linux system are break like python :( So no Python scripts Thanks for the explanation I have a little better advanced, but now I encounter another problem how to convert correctly into letter? I have that valor : "nA" Now i have a new prob : TypeError: ord() expected string of length 1, but int found Forget all that I have said now, why i have that valor : "nA" Hi Wavic Yes i know i have ubuntu in linux but my ubuntu/linux system are break like python :( So no Python scripts Thanks for the explanation I have a little better advanced, but now I encounter another problem how to convert correctly into letter? I have that valor : "nA" Sorry for the spam :( i can't delete all this... Cryptography problem codage - PYTHONEUR - Jan-22-2017 Hi, why this error : TypeError: ord() expected string of length 1, but int found Here the code you can test it fast in this website : http://www.pythontutor.com/visualize.html#mode=display Msg ="MATHS" def num (caractere): return ord (caractere)-65 def lettre (n): return ord(n)-65 for c in Msg : print (c,end="|") print() for c in Msg : print(num(c), end=" | ") print ('\n\n', 'le message est sous forme de chiffres') for c in Msg: print ( '\n\n','Maintenant veuillez entrer deux chiffres pour créer votre cles ') # Finir Cle print ('\n\n', 'soit la fonction affine a*x + b qui va vous permettre de créer votre cles ') A = 11 B = 7 for c in Msg: r= (A*(ord(c)-65)+ B)%26 + 65 print(r , end=" | " ) for c in Msg : print(lettre(r), end=" | " ) RE: Cryptography Problem - wavic - Jan-22-2017 Hello! It says: TypeError: ord() expected string of length 1, but int found ord(char) turns a letter into a number. You need chr(int) which gets a number and returns a characterIn [1]: chr(100) Out[1]: 'd' In [2]: |