LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2014, 11:19 AM   #1
nickygencs17
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Rep: Reputation: Disabled
Post python scripting question


I need some help with rot 13 script that encrypts and decrypts, it can only import sys and not using maketrans or lamda but using the key
Code:
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'}
what i have so far is

Code:
#!/usr/bin/python
import sy





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'}




if(len(sys.argv)==3):
        if (sys.argv[1] == "d"):
                for c in sys.argv[1]:
                        print (key['c'])




else
        print "Only filename and two arguments please"
        return
~                                                                                                                                                                                    
~                                                                                                                                                                                    
~
I've only just learned python and its very confusing i appreciate all the help and the constructive criticism thank you very much - nick
 
Old 05-06-2014, 02:06 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 05-06-2014, 05:17 PM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
The usage of your script is actually unclear. What are the two arguments the script accept? Anyway, to open a file, you need to use open function.
 
Old 05-06-2014, 11:18 PM   #4
nickygencs17
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-07-2014, 07:43 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
1 members found this post helpful.
Old 05-07-2014, 07:58 AM   #6
rscudder
LQ Newbie
 
Registered: Jan 2014
Location: Pennsylvania, US
Distribution: Slackware
Posts: 3

Rep: Reputation: Disabled
If that works for you, good. The comments aren't very helpful though. They should explain what is going on but is not obvious.

This line is a great example of poor use of a comments.

if c in key: #if c is in key
 
Old 05-07-2014, 09:31 AM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question : Python Scripting / Browser netlurk405 Red Hat 1 09-02-2012 06:44 AM
Question : Python Scripting / Browser netlurk405 Programming 3 09-01-2012 12:54 PM
LXer: Scripting KVM with Python, Part 2: Add a GUI to manage KVM with libvirt and Python LXer Syndicated Linux News 0 01-17-2012 08:11 PM
python scripting sammymcgee Programming 4 03-23-2011 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration