ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
And you're getting a row of the letter "p" as your result, aren't you?
Ask your instructor for further help with your homework, but, before you do, consider "literally" what you are right now asking the computer to do. You can quote me on that, in fact.
no sundialsvcs that was never the problem c is a character in the passed string and mina86 the arguments were d for decoding and e for encoding and the message to be en/de coded well anyway i worked on it and solved it myself here is the finished product but thanks for the help
Code:
#!/usr/bin/python
#Nicholas Genco
import sys #importing sys libary
stringname = "" #setting stringname to empty string
key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u',
'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c',
'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k',
'y':'l', 'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', 'F':'S',
'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', 'N':'A',
'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I',
'W':'J', 'X':'K', 'Y':'L', 'Z':'M'}
#making a dict called key
dkey=dict(zip(key.values(),key))
#reversing key
lenggthh=len(sys.argv)#length of sys.argv set to int
if (lenggthh == 3): #testing if length is greater then e
if (sys.argv[1] == "e" or sys.argv[1] == "E"): #testing if second argument is e
for c in sys.argv[2]: #encoding message
if c in key: #if c is in key
stringname=stringname+key[c] #add key of c to emptystring
else: #if stringname is special character
stringname=stringname+c #add special character
elif (sys.argv[1] == "d" or sys.argv[1] == "D"): #testing if second argument is d
for f in sys.argv[2]: #decoding message
if f in dkey: #testing if f is in dkey
stringname=stringname+dkey[f] #adding key of f into emptystring
else: #testing for special characters
stringname=stringname+f#adding special character
else:#else second argument is not d or e
print "ERROR: SECOND ARGUMENT IS NOT D/d or E/E"
sys.exit(12)#system exit
else:#else to arguments
print " MUST HAVE THREE ARGUMENTS"
sys.exit(9)#system exit
print stringname#printing stringname
sys.exit(0)#system exit 0
~
Last edited by nickygencs17; 05-06-2014 at 11:21 PM.
Well good to see you found a solution, although I would say the initial script was riddled with errors.
As for your new solution, my one gripe would be the ineffectual error message you return when someone enters the wrong number of arguments:
Code:
print " MUST HAVE THREE ARGUMENTS"
Whilst the novice user now realises that 3 arguments are required, they are given no indication as to what they might be or if there is any desired order.
Might be something to think about
Ah, now I see. I thought the script was supposed to operate on a file and not on the second argument.
Few notes on your code (this is in addition to what grail and rscudder have said):
1. You do not need parenthesis around if condition.
2. “dkey=dict(zip(key.values(),key))” -- I would fear that construct. I don't know if there are any guarantees that you'll get values and keys in the same order. I'd do “dkey = dict((b, a) for a, b in key.items())”.
2a. On that topic, when the string is being encoded, dkey is never used, and this computation is not needed. Perhaps it would be better to compute dkey only when it's needed?
3. You are aware that in rot-13 encoding and decoding is the same thing, right?
4. Check out dictionary's get method, e.g. “result += key.get(next_char, next_char)” allows you to get rid of the “if next_char in key” condition.
5. Instead of comparing to “d” and then to “D”, use “lower” method of a string.
6. Did you notice how encoding and decoding is the same code except one uses key and the other uses dkey dictionary? You should move that common code to a function and pass the string to operate on and the key as arguments.
7. You do not need sys.exit(0) at the end.
8. If you handle “len(sys.argv) != 3” case first, you can put the whole majority of the code out of if body. This means you save one indention level. This will make the code look less complex.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.