Creating a replacement cypher with triple-quoted strings

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

Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Creating a replacement cypher with triple-quoted strings

 
0
  #1
Sep 2nd, 2009
I'm attempting to write a program that will take a word entered by the user and give a concatenation of the corresponding triple quote strings.

I've looked at http://www.daniweb.com/forums/thread189881.html, but didn't quite find the piece I'm missing.

The idea is fairly simple:

User inputs a word (or numbers)
Exchanges letters for corresponding "symbols" (for lack of a better term)
Outputs concatenated symbols

Here's how I've got it set up so far:

  1. # Create a "normal" alphabet to compare it to
  2.  
  3. base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  4.  
  5. FIVEHIGH = (
  6. """
  7. X 0 X X
  8. 0 X 0 X
  9. 0 0 0 X
  10. 0 X 0 X
  11. 0 X 0 X
  12. """,
  13. """
  14. 0 0 X X
  15. 0 X 0 X
  16. 0 0 X X
  17. 0 X 0 X
  18. 0 0 X X
  19. """,
  20. """
  21. X 0 X X
  22. 0 X 0 X
  23. 0 X X X
  24. 0 X 0 X
  25. X 0 X X
  26. """)
  27. # Cut for the sake of space
  28.  
  29. ### PROGRAM START ###
  30.  
  31. cypher = string.maketrans(base, FIVEHIGH)
  32.  
  33. print "Welcome to the Word Converter!\n\n"
  34.  
  35. # OTHER DEFINITIONS
  36.  
  37. word = raw_input("What word should we convert? ").upper()
  38.  
  39. # FEEDBACK
  40.  
  41. if word == "":
  42. print "No word entered"
  43. else:
  44. print "Your word is",word
  45. word.translate(cypher)
  46. print word
  47.  
  48. raw_input("\nPress any key")

I've also toyed with dictionaries, but couldn't get them to work either. Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #2
Sep 2nd, 2009
Originally Posted by Warkthogus View Post
I've also toyed with dictionaries, but couldn't get them to work either. Any help would be greatly appreciated.
When "it doesn't work" in python, most of the time, there is an exception traceback (python complains). Here is what I get in my terminal when I try to run your program
  1. [t215809] python cypher.py
  2. Traceback (most recent call last):
  3. File "cypher.py", line 31, in <module>
  4. cypher = string.maketrans(base, FIVEHIGH)
  5. NameError: name 'string' is not defined
Python tells us that when it reaches the line cypher = string.maketrans(base, FIVEHIGH) , it fails because you didn't say what the variable "string" is. When it doesn't work, you must study the traceback and understand what it means. Now your problem is to tell python what "string" is. After this problem, there are other problems waiting for you, with other tracebacks.
So, go one step after the other, and when you have a traceback, put it in your posts because it's very useful for helpers.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 56
Reputation: willygstyle is an unknown quantity at this point 
Solved Threads: 6
willygstyle willygstyle is offline Offline
Junior Poster in Training

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #3
Sep 2nd, 2009
"".stringmethod() works if you need an empty string to use the string methods to.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #4
Sep 2nd, 2009
word = raw_input("What word should we convert? ").upper()
word.translate(cypher)
If the first statement, you define 'word' as a string. In the second statement, it appears to be a class with the method "translate". You can not use the same variable name for two different types of memory.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #5
Sep 3rd, 2009
Originally Posted by Gribouillis View Post
Python tells us that when it reaches the line cypher = string.maketrans(base, FIVEHIGH) , it fails because you didn't say what the variable "string" is. When it doesn't work, you must study the traceback and understand what it means. Now your problem is to tell python what "string" is. After this problem, there are other problems waiting for you, with other tracebacks.
So, go one step after the other, and when you have a traceback, put it in your posts because it's very useful for helpers.
I apologize for not giving you guys more information. I forgot to mention that at the very beginning, I have:
  1. import string
Originally Posted by woooee View Post
If the first statement, you define 'word' as a string. In the second statement, it appears to be a class with the method "translate". You can not use the same variable name for two different types of memory.
Thanks woooee. I went back, and fixed it to say string.translate(word,cypher).

With the changes I've made, here's what I've got:
  1. import string
  2.  
  3. base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  4. FIVEHIGH = (
  5. """
  6. X 0 X X
  7. 0 X 0 X
  8. 0 0 0 X
  9. 0 X 0 X
  10. 0 X 0 X
  11. """,
  12. """
  13. 0 0 X X
  14. 0 X 0 X
  15. 0 0 X X
  16. 0 X 0 X
  17. 0 0 X X
  18. """,
  19. """
  20. X 0 X X
  21. 0 X 0 X
  22. 0 X X X
  23. 0 X 0 X
  24. X 0 X X
  25. """)
  26. #Goes through "Z," then 1-0
  27.  
  28. cypher = string.maketrans(base, FIVEHIGH)
  29.  
  30. print "Welcome to the Converter!\n\n"
  31.  
  32. word = raw_input("What word should we make? ").upper()
  33.  
  34. # FEEDBACK
  35.  
  36. if word == "":
  37. print "No word entered"
  38. else:
  39. print "Your word is",word
  40. string.translate(word,cypher)
  41. print word
  42.  
  43. raw_input("\nPress any key")
When run, I get the traceback:
  1. cypher = string.maketrans(base, FIVEHIGH)
  2. TypeError: maketrans() argument 2 must be string or read-only character buffer, not tuple
Now for my question: How would I make a string or read-only character buffer out of triple quoted strings?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #6
Sep 3rd, 2009
Look at the documentation of maketrans. It can only replace a character by a single character. This means that you can't use maketrans and translate for your purpose. Here is how you could iterate over the characters
  1. if word == "":
  2. print "No word entered"
  3. else:
  4. print "Your word is",word
  5. for c in word:
  6. print(c)
Now, instead of printing the character, you must find the triple quoted string which corresponds to this character (if any). You should then put all these strings in a list and finally concatenate all the strings of the list.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #7
Sep 3rd, 2009
Originally Posted by Gribouillis View Post
Now, instead of printing the character, you must find the triple quoted string which corresponds to this character (if any). You should then put all these strings in a list and finally concatenate all the strings of the list.
Alright, I've got the strings concatenated (which took a while), but I'm still stuck with the question I started with:

How would I get the letters to know which triple quoted string corresponds to it?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #8
Sep 3rd, 2009
You can create a dictionary to associate a string to each letter
  1. translation = dict()
  2. for i in range(len(base)):
  3. translation[base[i]] = FIVEHIGH[i]
and then use translation[c] to get the string corresponding to c. Post your new code when it's written.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #9
Sep 3rd, 2009
Alright, now we're getting somewhere!

Here's a shorter version of the program with only the letters A, B, and C used (for easier testing and conversion of symbols list):
  1. # DEFINE VARIABLES
  2.  
  3. base = "ABC"
  4. FIVEHIGH = (
  5. """
  6. X 0 X X
  7. 0 X 0 X
  8. 0 0 0 X
  9. 0 X 0 X
  10. 0 X 0 X
  11. """ +
  12. """
  13. 0 0 X X
  14. 0 X 0 X
  15. 0 0 X X
  16. 0 X 0 X
  17. 0 0 X X
  18. """ +
  19. """
  20. X 0 X X
  21. 0 X 0 X
  22. 0 X X X
  23. 0 X 0 X
  24. X 0 X X
  25. """)
  26.  
  27. translation = dict()
  28. for l in range(len(base)):
  29. translation[base[l]] = FIVEHIGH[l]
  30.  
  31. #PROGRAM START
  32.  
  33. print "Welcome to the Converter!\n\n"
  34.  
  35. word = raw_input("What word should we make? ").upper()
  36.  
  37. # FEEDBACK
  38.  
  39. if word == "":
  40. print "No word entered"
  41. else:
  42. print "Your word is",word
  43. for c in word:
  44. print translation[c]
  45.  
  46. raw_input("\nPress any key")
I got pretty excited because I didn't get any errors, but then this is what happened:
  1. Welcome to the Converter!
  2.  
  3.  
  4. What word should we make? cab
  5. Your word is CAB
  6.  
  7.  
  8.  
  9. X
  10.  
  11. Press any key
What happened to my gorgeous, museum-worthy ASCII letters? I read over the code several times, and I just can't see where the rest of the strings got swallowed.


One other minor question. I understand very basically what the below code is doing, but how does it work in the background (or how would you say it in layman's terms)?

translation = dict()
for l in range(len(base)):
    translation[base[l]] = FIVEHIGH[l]
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #10
Sep 3rd, 2009
You must not add the triple quoted strings in FIVEHIGH.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1294 | Replies: 29
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC