I have programed before (QuickBASIC and Macromedia Flash MX) but I am new on Python

I want to create a program in which you give it a keyword and it generates a cypher.

Like this:

(Built in alphabet)
(Asks for keyword)
(Lets say the keyword is BLOCK)
(Adds the keyword to the beggining of the alphabet)
(Removes repeated letters)
(Prints coded alphabet along with the original alphabet)
(And maybe asks for a displacement value)
(Lets say the displacement value is 5)
(Displaces the coded alphabet)
(Prints the coded and displaced alphabet along with the original alphabet)

Here is my code so far:

alphabet = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]
print "Alphabet"
print alphabet
# Lets get the keyword
keyword = raw_input ("What is the keyword?: " )
print keyword
keyword = keyword.upper()
print keyword
print alphabet
# Now somehow remove repeated letters
# And place the keyword at the beggining

By the way I have Python 2.6.2

Python makes this sort of thing easy.

keyword = raw_input().upper()

# You may want to check that the keyword contains only letters from A-Z
# if not keyword.isalpha():
#    do something
for letter in keyword: # remove keyword letters from alphabet
    alphabet = alphabet.replace(letter, "")

d = int(raw_input()) #displacement value/replace with stronger integer retrieval code. strVar.isdecimal() can help here.
cipher = alphabet[-d:] + keyword+ alphabet[:-d]

I can't understand a lot of that code, sorry. Would you care to explain?


Instead of using a list, I just use a string because it's still a sequence (can be iterated, indexed), and I didn't need any of the extra list features.

for letter in keyword: # remove keyword letters from alphabet
    alphabet = alphabet.replace(letter, "")

Here, for each of the letters in the keyword, I remove that letter from the alphabet string (by replacing it with nothing).

d = int(raw_input())

I get the "displacement value" from the user and convert it to an integer. Note, you need to do this in a better way, because if the user enters anything that isn't a number, the code will break.

cipher = alphabet[-d:] + keyword+ alphabet[:-d]

This is probably the only part I can see you having trouble with. What I'm doing is that I'm using slicing to split the alphabet string up according to the displacement value.

alphabet[-d:] means give me the last d characters of alphabet
alphabet[:d-] means give me everything up to the last d characters of alphabet
...where d is a number.

The use of + on strings and/or sequences is called concatenation. It joins both sequences together. Example:
"Foo" + "Bar" = "FooBar"

Thank you, I understand that now.

I like using sets because they have a difference function.

keyword = raw_input("Enter word ").upper()
difference = alphabet.difference(keyword)

## sets are in hash order not in alphabetical order
print "Hash sequence", difference, "\n"

## convert to a list so it can be sorted
difference = list(difference)    

## now it is in alphabetical order
print "Alpha order", difference, "\n"

## create a new list with the keyword first
final_list = list(keyword) + difference
print "Final_list", final_list
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.