-2

I obtained the following password generator (Test.py) from the Internet and I thought it would be easy to run in Canopy but it does not seem to work at all.

I get invalid syntax errors all the time. I send the code to a colleague of mine and she said it worked fine using the simple i-Python terminal on her Linux Machine.

#!/usr/bin/env python
import random
import sys
import string


def main(argv):

    if (len(sys.argv) != 5):
        sys.exit('Usage: simple_pass.py <upper_case> <lower_case> <digit> <special_characters>')

    password = ''

    for i in range(len(argv)):
        for j in range(int(argv[i])):
            if i == 0:
                password += string.uppercase[random.randint(0,len(string.uppercase)-1)]
            elif i == 1:
                password += string.lowercase[random.randint(0,len(string.lowercase)-1)]
            elif i == 2:
                password += string.digits[random.randint(0,len(string.digits)-1)]
            elif i == 3:
                password += string.punctuation[random.randint(0,len(string.punctuation)-1)]

    print 'Your new password is: ' + ''.join(random.sample(password,len(password)))

if __name__ == "__main__":
    main(sys.argv[1:])

I tried:

$python Test.py 2 2 2 2

I tried using the entire pathname as well, but nothing seems to work! I'm quite frustrated because it seems like it would be so easy. Any help would be greatly appreciated.

Leb
  • 15,483
  • 10
  • 56
  • 75
IljaPilja
  • 15
  • 1
  • 4
  • 3
    "I get invalid syntax errors all the time." Good starting point. Let's see the stack trace. Also, what version of Python are you using? – Kevin Jun 12 '15 at 16:57
  • I just tried your code and it turned out `Your new password is: 5HhJk+,0`. I think this is an expected output. – Old Panda Jun 12 '15 at 17:20
  • Why do you need anything _but_ the `if __name__ == '__main__'` line and some dummy contents, if that's what causes your syntax error? – Charles Duffy Jun 12 '15 at 17:25

1 Answers1

2

My guess is that you're running this command inside of ipython

$ python Test.py 2 2 2 2

Which is how you would run the script if you were in the command line, and it's comming back with the error:

In [1]: python Test.py 2 2 2 2
  File "<ipython-input-3-5bade39d33f0>", line 1
    python Test.py 2 2 2 2
                                           ^
SyntaxError: invalid syntax

If you want to run it inside of Canopy (which is an ipython shell, not a terminal window), run the file like so

In [1]: %run Test.py 2 2 2 2
Your new password is: NDt1q.+1
Ben
  • 6,986
  • 6
  • 44
  • 71
  • What Ben said. If this is not what's happening, then as others have said you need to show what's actually happening. If you are actually inside of Canopy and have the file open in the editor, just click the Run button. This will show you the ipython run command. Press the up arrow to recall the command, and add your command line argument, and press Enter. You can also set the command line arguments from the Canopy Run menu. – Jonathan March Jun 13 '15 at 00:08