Python-Cypher Generator

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 8
Reputation: David.lewing is an unknown quantity at this point 
Solved Threads: 0
David.lewing David.lewing is offline Offline
Newbie Poster

Python-Cypher Generator

 
0
  #1
May 1st, 2009
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:

  1. 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"]
  2. print "Alphabet"
  3. print alphabet
  4. # Lets get the keyword
  5. keyword = raw_input ("What is the keyword?: " )
  6. print keyword
  7. keyword = keyword.upper()
  8. print keyword
  9. alphabet.append(keyword)
  10. print alphabet
  11. # Now somehow remove repeated letters
  12. # 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Python-Cypher Generator

 
0
  #2
May 1st, 2009
Python makes this sort of thing easy.

  1. alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2. keyword = raw_input().upper()
  3.  
  4. # You may want to check that the keyword contains only letters from A-Z
  5. # if not keyword.isalpha():
  6. # do something
  7. for letter in keyword: # remove keyword letters from alphabet
  8. alphabet = alphabet.replace(letter, "")
  9.  
  10. d = int(raw_input()) #displacement value/replace with stronger integer retrieval code. strVar.isdecimal() can help here.
  11. cipher = alphabet[-d:] + keyword+ alphabet[:-d]
Last edited by scru; May 1st, 2009 at 3:17 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: David.lewing is an unknown quantity at this point 
Solved Threads: 0
David.lewing David.lewing is offline Offline
Newbie Poster

Re: Python-Cypher Generator

 
0
  #3
May 1st, 2009
I can't understand a lot of that code, sorry. Would you care to explain?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Python-Cypher Generator

 
0
  #4
May 1st, 2009
  1. 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.
  1. for letter in keyword: # remove keyword letters from alphabet
  2. 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).

  1. 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.

  1. 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"
Last edited by scru; May 1st, 2009 at 3:44 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: David.lewing is an unknown quantity at this point 
Solved Threads: 0
David.lewing David.lewing is offline Offline
Newbie Poster

Re: Python-Cypher Generator

 
0
  #5
May 1st, 2009
Thank you, I understand that now.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Python-Cypher Generator

 
0
  #6
May 1st, 2009
I like using sets because they have a difference function.
  1. alphabet = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  2. keyword = raw_input("Enter word ").upper()
  3. difference = alphabet.difference(keyword)
  4.  
  5. ## sets are in hash order not in alphabetical order
  6. print "Hash sequence", difference, "\n"
  7.  
  8. ## convert to a list so it can be sorted
  9. difference = list(difference)
  10. difference.sort()
  11.  
  12. ## now it is in alphabetical order
  13. print "Alpha order", difference, "\n"
  14.  
  15. ## create a new list with the keyword first
  16. final_list = list(keyword) + difference
  17. print "Final_list", final_list
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC