| | |
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:
Solved Threads: 0
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:
I've also toyed with dictionaries, but couldn't get them to work either. Any help would be greatly appreciated.
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:
Python Syntax (Toggle Plain Text)
# Create a "normal" alphabet to compare it to base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" FIVEHIGH = ( """ X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X """, """ 0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X """, """ X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X """) # Cut for the sake of space ### PROGRAM START ### cypher = string.maketrans(base, FIVEHIGH) print "Welcome to the Word Converter!\n\n" # OTHER DEFINITIONS word = raw_input("What word should we convert? ").upper() # FEEDBACK if word == "": print "No word entered" else: print "Your word is",word word.translate(cypher) print word 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.
•
•
•
•
I've also toyed with dictionaries, but couldn't get them to work either. Any help would be greatly appreciated.
Python Syntax (Toggle Plain Text)
[t215809] python cypher.py Traceback (most recent call last): File "cypher.py", line 31, in <module> cypher = string.maketrans(base, FIVEHIGH) NameError: name 'string' is not defined
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.
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
•
•
•
•
word = raw_input("What word should we convert? ").upper()
word.translate(cypher)
Linux counter #99383
•
•
Join Date: Sep 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
Python tells us that when it reaches the linecypher = 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.
Python Syntax (Toggle Plain Text)
import string
•
•
•
•
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.
With the changes I've made, here's what I've got:
Python Syntax (Toggle Plain Text)
import string base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" FIVEHIGH = ( """ X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X """, """ 0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X """, """ X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X """) #Goes through "Z," then 1-0 cypher = string.maketrans(base, FIVEHIGH) print "Welcome to the Converter!\n\n" word = raw_input("What word should we make? ").upper() # FEEDBACK if word == "": print "No word entered" else: print "Your word is",word string.translate(word,cypher) print word raw_input("\nPress any key")
Python Syntax (Toggle Plain Text)
cypher = string.maketrans(base, FIVEHIGH) TypeError: maketrans() argument 2 must be string or read-only character buffer, not tuple
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
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.
python Syntax (Toggle Plain Text)
if word == "": print "No word entered" else: print "Your word is",word for c in word: print(c)
•
•
Join Date: Sep 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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.
How would I get the letters to know which triple quoted string corresponds to it?
You can create a dictionary to associate a string to each letter
and then use
python Syntax (Toggle Plain Text)
translation = dict() for i in range(len(base)): translation[base[i]] = FIVEHIGH[i]
translation[c] to get the string corresponding to c. Post your new code when it's written. •
•
Join Date: Sep 2009
Posts: 16
Reputation:
Solved Threads: 0
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):
I got pretty excited because I didn't get any errors, but then this is what happened: 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)?
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):
Python Syntax (Toggle Plain Text)
# DEFINE VARIABLES base = "ABC" FIVEHIGH = ( """ X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X """ + """ 0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X """ + """ X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X """) translation = dict() for l in range(len(base)): translation[base[l]] = FIVEHIGH[l] #PROGRAM START print "Welcome to the Converter!\n\n" word = raw_input("What word should we make? ").upper() # FEEDBACK if word == "": print "No word entered" else: print "Your word is",word for c in word: print translation[c] raw_input("\nPress any key")
Python Syntax (Toggle Plain Text)
Welcome to the Converter! What word should we make? cab Your word is CAB X Press any key
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]![]() |
Similar Threads
- Starting Python (Python)
- Lisp load function in windows (Computer Science)
- Creating a Basic String Database (C++)
- Replacement Site - Old Links? (Site Layout and Usability)
- Cryptography In C (C)
- Odd string concatenation behavior? (Perl)
- syntax errors and the check module command? (Python)
- Problem with Perl syntax highlighter (DaniWeb Community Feedback)
- regular expressions (Java)
- Desiging a set of rules for a match (C++)
Other Threads in the Python Forum
- Previous Thread: Read between 2 keywords in file
- Next Thread: building pymedia
Views: 1294 | Replies: 29
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






