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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arturo trafny
    New Member
    • Mar 2011
    • 2

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

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

    Traceback (most recent call last):
    File "C:\Users\a\Des ktop\checkerboa rd", line 26, in <module>
    print(checkerbo ard(plaintext))
    File "C:\Users\a\Des ktop\checkerboa rd", line 20, in checkerboard
    ciphertext=ciph ertext+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.

    Code:
    def checkerboard(plaintext):
        string1='estonia r '
        string2='bcdfghjklm'
        string3='pquvwxyz,/'
        plainstripped=''
        for ch in plaintext:
            if ch.isalpha():
                plainstripped=plainstripped+ch
            plainstripped=plainstripped.lower()
        ciphertext=' '
        for ch in plainstripped:
            if ch in string1:
                idx=string1.index(ch)
                ciphertext=ciphertext+idx
            elif ch in string2:
                idx=string2.index(ch)
                ciphertext=ciphertext+idx
            else:
                idx=string3.index(ch)
                ciphertext=ciphertext+idx
        return ciphertext
    
    
    plaintext= 'What is Going On123'
    
    print(checkerboard(plaintext))
    Last edited by bvdet; Mar 2 '11, 01:46 PM.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to convert idx to a string before you try to append it

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Rabbit is correct, but I would use the term concatenation instead of append.

      Comment

      • arturo trafny
        New Member
        • Mar 2011
        • 2

        #4
        thank you, i was stuck on that for so long, something so small i totally forgot about.

        I have one more question, in string1='estoni a 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.
        Last edited by arturo trafny; Mar 2 '11, 06:00 PM. Reason: more info.

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          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, "plainstrip ped" 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.
          Code:
          def checkerboard(plaintext):
              string1='estonia r '
              string2='bcdfghjklm'
              string3='pquvwxyz,/'
              ciphertext=[]               ## using list instead of string
              for ch in plaintext:
                  if ch.isalpha():
                      ch = ch.lower()
                      if ch in string1:
                          ciphertext.append(str(string1.index(ch)))
                      elif ch in string2:
                          ciphertext.append(str(string2.index(ch)))
                      elif ch in string3:
                          ciphertext.append(str(string3.index(ch)))
                      else: 
                          print ch, "not in strings"
              return "".join(ciphertext)
           
           
          plaintext= 'What is Going On123'
          print(checkerboard(plaintext))

          Comment

          Working...