| | |
Python-Cypher Generator
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
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)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(Asks for keyword)
(Lets say the keyword is BLOCK)
BLOCK
(Adds the keyword to the beggining of the alphabet)
BLOCKABCDEFGHIJKLMNOPQRSTUVWXYZ
(Removes repeated letters)
(Prints coded alphabet along with the original alphabet)
BLOCKADEFGHIJMNPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(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)
VWXYZBLOCKADEFGHIJMNPQRSTU
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is my code so far:
By the way I have Python 2.6.2
I want to create a program in which you give it a keyword and it generates a cypher.
Like this:
(Built in alphabet)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(Asks for keyword)
(Lets say the keyword is BLOCK)
BLOCK
(Adds the keyword to the beggining of the alphabet)
BLOCKABCDEFGHIJKLMNOPQRSTUVWXYZ
(Removes repeated letters)
(Prints coded alphabet along with the original alphabet)
BLOCKADEFGHIJMNPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(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)
VWXYZBLOCKADEFGHIJMNPQRSTU
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is my code so far:
Python Syntax (Toggle Plain Text)
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 alphabet.append(keyword) print alphabet # Now somehow remove repeated letters # And place the keyword at the beggining
By the way I have Python 2.6.2
Last edited by David.lewing; May 1st, 2009 at 2:54 pm.
Python makes this sort of thing easy.
python Syntax (Toggle Plain Text)
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 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]
Last edited by scru; May 1st, 2009 at 3:17 pm.
python Syntax (Toggle Plain Text)
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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.
python Syntax (Toggle Plain Text)
for letter in keyword: # remove keyword letters from alphabet alphabet = alphabet.replace(letter, "")
python Syntax (Toggle Plain Text)
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.
python Syntax (Toggle Plain Text)
cipher = alphabet[-d:] + keyword+ alphabet[:-d]
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"
Last edited by scru; May 1st, 2009 at 3:44 pm.
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 297
I like using sets because they have a difference function.
Python Syntax (Toggle Plain Text)
alphabet = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ") 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) difference.sort() ## 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
![]() |
Other Threads in the Python Forum
- Previous Thread: Python good for anything standalone?
- Next Thread: Problems with embedded javascript in buttons.
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






