Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cryptography Problem
#1
Please Help me what's the prob with str and int 

If someone can explain this problem  Confused 

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=" | " )
Reply
#2
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
Reply
#3
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
Reply
#4
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' + 5
 you need to convert the integer to a string first like:
mystring = 'Abcdefg' + '5'
Reply
#5
Oh "they" I just told the developers while it's the program lol Big Grin

Yes i use python On-line, i don't have python because i have Ubuntu  Undecided
 
But excuse me i don't fully understand  Huh

The prob is in return chr 

how i can use correctly int and str, i test the two and it's not work. Dodgy

 return chr ('n'+'65')
Reply
#6
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')
Reply
#7
(Jan-22-2017, 06:13 AM)Larz60+ Wrote: es i use python On-line, i don't have python because i have Ubuntu
You 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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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...
Reply
#9
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=" | " )
    

   


User has been warned for this post. Reason: creating unnecessary new threads
Reply
#10
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 character

In [1]: chr(100)
Out[1]: 'd'

In [2]:
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Cryptography Capture the Flag DrEastwood 12 11,526 Oct-12-2016, 11:40 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020