473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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\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
4 4053
Rabbit
12,516 Expert Mod 8TB
You need to convert idx to a string before you try to append it
Mar 2 '11 #2
bvdet
2,851 Expert Mod 2GB
Rabbit is correct, but I would use the term concatenation instead of append.
Mar 2 '11 #3
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
dwblas
626 Expert 512MB
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

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

Similar topics

6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
4
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code...
7
by: Piotr Turkowski | last post by:
Hi! Here you can get some notes about Vigenere Cipher: http://raphael.math.uic.edu/~jeremy/crypt/vignere.html Here's whole code of my program, function stats() is in polish, so you can omit...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
1
by: beetle17 | last post by:
Plaintext: a  n i c e  d a y Key: -3 Ciphertext: X  k f Z b  a X v Cipher will accept commands from the user and perform the operations required by the commands. There are three different...
4
by: wagn31 | last post by:
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:
2
by: yeruvavasavi | last post by:
Algorithm 1: Write python programs to translate the following algorithm so that the computer can execute it. Algorithm for ceaser cipher. Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ<br> Cipher: ...
0
by: ntuyen01 | last post by:
Hi all, I want to use the SSLStream with the cipher (TLS_RSA_WITH_AES_128_CBC_SHA) to get the handshake with my server, but I not sure where I can start. I do it in C# 2.0 Here is my code: ...
7
by: mcollins1989 | last post by:
Draw a checkerboard (8 by 8 squares) out of vertical lines and underscores. You must use two nested for loops, and the for loops should each execute 8 times, e.g. for (int x = 0; x < 8; x++) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.