By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

How to fix Can't convert 'int' object error for checkerboard cipher?

P: 2
I am getting the following error:
##this is the error message i receive

Traceback (most recent call last):
File "C:\Users\a\Desktop\checkerboard", line 26, in <module>
print(checkerboard(plaintext))
File "C:\Users\a\Desktop\checkerboard", line 20, in checkerboard
ciphertext=ciphertext+idx
TypeError: Can't convert 'int' object to str implicitly

I am running out of ideas to make this thing work. I have to complete this encryption cipher with the number representing each letter corresponding to the index of the letter in one of the three strings.

For string2 and 3 however i have to have the index corresponding to the first space in string1 at the front of the index number corresponding to each of those letters in string2 and the index of the second space in string1 in front of the index number for letters corresponding to string3. That part i haven't quite figured out yet.

Expand|Select|Wrap|Line Numbers
  1. def checkerboard(plaintext):
  2.     string1='estonia r '
  3.     string2='bcdfghjklm'
  4.     string3='pquvwxyz,/'
  5.     plainstripped=''
  6.     for ch in plaintext:
  7.         if ch.isalpha():
  8.             plainstripped=plainstripped+ch
  9.         plainstripped=plainstripped.lower()
  10.     ciphertext=' '
  11.     for ch in plainstripped:
  12.         if ch in string1:
  13.             idx=string1.index(ch)
  14.             ciphertext=ciphertext+idx
  15.         elif ch in string2:
  16.             idx=string2.index(ch)
  17.             ciphertext=ciphertext+idx
  18.         else:
  19.             idx=string3.index(ch)
  20.             ciphertext=ciphertext+idx
  21.     return ciphertext
  22.  
  23.  
  24. plaintext= 'What is Going On123'
  25.  
  26. print(checkerboard(plaintext))
Mar 2 '11 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,374
You need to convert idx to a string before you try to append it
Mar 2 '11 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Rabbit is correct, but I would use the term concatenation instead of append.
Mar 2 '11 #3

P: 2
thank you, i was stuck on that for so long, something so small i totally forgot about.

I have one more question, in string1='estonia r '. when the encryption encrypts a character for string2 or 3. before the index of the character it has to print the index corresponding to the first space in string 1 for string2 and the second space in string1 for string 3.

so like W would be 94 not just 4, because the last space in string1 is at index 9. and then the index of w in string3 is 4.

any help on that would be really appreciated. thank you.
Mar 2 '11 #4

Expert 100+
P: 621
You can use the find() function to lookup characters in a string. It also takes an optional start and/or end parameters so you would do 3 lookups to find the third space (or process the string once yourself). Also, "plainstripped" is not necessary from the code you posted. And what happens it the letter is not in string1, string2, or string3, or in two or more strings?. Finally, it is a good idea to get used to appending characters to a list instead of concatenating strings as it is more efficient in Python.
Expand|Select|Wrap|Line Numbers
  1. def checkerboard(plaintext):
  2.     string1='estonia r '
  3.     string2='bcdfghjklm'
  4.     string3='pquvwxyz,/'
  5.     ciphertext=[]               ## using list instead of string
  6.     for ch in plaintext:
  7.         if ch.isalpha():
  8.             ch = ch.lower()
  9.             if ch in string1:
  10.                 ciphertext.append(str(string1.index(ch)))
  11.             elif ch in string2:
  12.                 ciphertext.append(str(string2.index(ch)))
  13.             elif ch in string3:
  14.                 ciphertext.append(str(string3.index(ch)))
  15.             else: 
  16.                 print ch, "not in strings"
  17.     return "".join(ciphertext)
  18.  
  19.  
  20. plaintext= 'What is Going On123'
  21. print(checkerboard(plaintext)) 
Mar 2 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.