I have made a cipher substitution program which would take some input and print a coded output.
however,the required output is not coming.
My program first asks for input.then it converts it into a list as it is mutable.ALso,a string of all the alphabets is declared too.

``````#cipher code substitution program
I = raw_input("Enter message:")
Alpha = "abcdefghijklmnopqrstuvwxyz"
I = list(I)
count=0

for x in Alpha:
y = 25- count
while x in I > 0:
I[I.index(x)] = Alpha[y]

count += 1
continue

print(I)``````

for loop is used which will iterate over the alpha string one by one.I have used a counter to number the alphabets 0-a 1-b and so on.The cipher formula is the {y=25 - count}.So a corresponds to z. etc
The while loop checks if x(alphabet) is in l (list of input) and executes the loop.I used I.index(x) so it gives the index of the first occurrence of x and the corresponding cipher is assigned through Alpha[y].
This will continue till all the alphabets are done .
Now I want the program to take my input and encode it like this:
I type 'abc' the output would be 'zyx'
However it gives :

``````Enter message:abc
['a', 'b', 'c']``````

What is the problem with my program?If you can suggest a simpler and efficient way to do cipher substitution please do so.I am kind of a beginner too.

## All 3 Replies

For future reference there isn't much response for someone who can solve there own problem with a few print statements. This will get you started but there are errors in your logic.

``````for x in Alpha:
y = 25- count
print "first x, y", x,y
while x in I > 0:
print "     second x-->replacing", I[I.index(x)], "with", Alpha[y]
I[I.index(x)] = Alpha[y]
print "     ", I
count += 1
continue

print(I)``````

Your code is bass-ackwards in that you want to iterate through the input and then find the letter in the a-z string, and it is usually better to append the new letter to a second list instead of replacing letters in a list that you iterate over.

``````from_input = raw_input("Enter message: ")
Alpha = "abcdefghijklmnopqrstuvwxyz"

for ltr in from_input:
## allow for capital letters
ltr=ltr.lower()
count = Alpha.index(ltr)
y = 25- count
print ltr, count, Alpha[y]``````

For future reference there isn't much response for someone who can solve there own problem with a few print statements. This will get you started but there are errors in your logic.

``````for x in Alpha:
y = 25- count
print "first x, y", x,y
while x in I > 0:
print "     second x-->replacing", I[I.index(x)], "with", Alpha[y]
I[I.index(x)] = Alpha[y]
print "     ", I
count += 1
continue

print(I)``````

Your code is bass-ackwards in that you want to iterate through the input and then find the letter in the a-z string, and it is usually better to append the new letter to a second list instead of replacing letters in a list that you iterate over.

``````from_input = raw_input("Enter message: ")
Alpha = "abcdefghijklmnopqrstuvwxyz"

for ltr in from_input:
## allow for capital letters
ltr=ltr.lower()
count = Alpha.index(ltr)
y = 25- count
print ltr, count, Alpha[y]``````

actually when I typed in the alphabets for testing i got this weird output:

``````Enter message:abcdefghijklmnopqrstuvwxyz
abcdefghijklmmlkjihgfedcba
>>>``````

Ok but then I tested your program and its good.Somehow I was unable to figure out what my code lacked.Can you explain?

I already did with the print statements in the first code snippet. Enter "ab" only so do don't get tons of output. Hint: the problem is caused by changing the list you are iterating over.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.