[Python] Random Password Generator

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
Yeah, it was easy to do. I just felt like posting it here.
PHP:
from time import sleep
import sys
import random
x = 0
stack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
while x == 0:
    print 'Random Password Generator'
    print
    sleep(0.1)
    print 'Password length?'
    length = input(">>>")
    loop = 0
    password = ""
    if length > 0:
        while loop != length:
            rand = random.randint(0,61)
            password = password + stack[rand]
            loop = loop + 1
        print password
    else:
        print "Error, make sure you only enter positive numbers."
    sleep(0.5)
    print
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Yeah, it was easy to do.
Code:
rand = random.randint(1,61)

Maybe it's not that easy:
For some weird reason, none of the passwords happens to contain an 'a'. Now where could that come from? :rolleyes:

EDIT: and one'd better not enter negative numbers, but maybe I'm too picky.
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
Oh, yeah. I did this a long time ago and then I made the random number between 1 and 62. It crashed occasionally though. So I changed it to 61 since it starts from 0. I forgot to change the first number though.
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
I know, fixing it in a sec. My python crashed when I tried to enter a negative number without thinking :rolleyes:

Edited it. How come it crashes if I change it to raw_input instead of input?
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
You'd better convert that input to an integer...
Code:
try:   
    length = int(raw_input(">>>"), 10)
except ValueError:
    length = 0
Or use atoi(), ...
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
Not automatically.

PHP:
>>> test = raw_input(">")
>15
>>> test + 5
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    test + 5
TypeError: cannot concatenate 'str' and 'int' objects
>>>
 

Artificial

Without Intelligence
Reaction score
326
> Not automatically.
He was probably talking about atoi:
> What does atoi do?
> Fairly sure it converts a string into an integer.

But I think it's depracted:
[url=http://www.python.org/doc/2.5.2/lib/node42.html]Here[/url] said:
atoi( s[, base])
Deprecated since release 2.0. Use the int() built-in function.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top