![]() |
|
Password Generator - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Password Generator (/Thread-Password-Generator) Pages:
1
2
|
Password Generator - Equinox - 01-26-2014 Imports For this project we will need "os", "random", and "string". Code: import os
import random
import stringDefinitions You only need to define one thing for this project. The Close event. Code: def close(event):
os.system('taskkill /PID %d /F' % os.getpid())Generator Code: length = random.randint(8,30)
chars = string.ascii_letters + string.digits + '~!@#$%^&*()_+'
random.seed = (os.urandom(1024))Printing and re-generating All we need to do now is print the password and prompt the user if they want a new one. Code: password = print("[PASSGEN]Generating password.\n[PASSGEN]Password generated:\n" + " ".join(random.choice(chars) for i in range(length)).replace(" ",""))
answer = input("[PASSGEN]Would you like to generate a new password? ")
if answer == "yes":
os.startfile("password generator.py"),close(password)
elif answer == "no":
close(password)It's very short and simple. Code: import os
import random
import string
import time
def close(event):
os.system('taskkill /PID %d /F' % os.getpid())
length = random.randint(8,30)
chars = string.ascii_letters + string.digits + '~!@#$%^&*()_+'
random.seed = (os.urandom(1024))
password = print("[PASSGEN]Generating password.\n[PASSGEN]Password generated:\n" + " ".join(random.choice(chars) for i in range(length)).replace(" ",""))
answer = input("[PASSGEN]Would you like to generate a new password? ")
if answer == "yes":
os.startfile("password generator.py"),close(password)
elif answer == "no":
close(password)Hope you liked the generator I coded. I think I am getting better everytime, especially being a Python noob. RE: Password Generator - Eclipse - 01-26-2014 Thanks for the release! Hope you had fun making it. I mostly just use an opensource one from sourceforge though. RE: Password Generator - Equinox - 01-26-2014 (01-26-2014, 09:58 AM)Sparks Wrote: Thanks for the release! Hope you had fun making it. I mostly just use an opensource one from sourceforge though. Course I had fun. I love programming. Even though I partially suck at it. xD
RE: Password Generator - Eclipse - 01-26-2014 (01-26-2014, 09:59 AM)Duubz Wrote: Course I had fun. Lol same here
RE: Password Generator - Equinox - 01-26-2014 TL;DR: RE: Password Generator - w00t - 01-26-2014 Why are you using os functions to open/close? Just recursively call a function with all the generation code in it if the user wants another password. Instead of random.choice(chars) do Code: chr(random.randint(94) + 32 )It is faster, uses less code, and looks neater. (94 printable english characters in ascii, they start at 32) RE: Password Generator - cr33pyguy - 01-26-2014 I remember making one of these back a year... It was simpler I think... A lot simpler. And n00bish. However, it also took in user input for the length (or you could enter random, in which case the length was random) anyway, this is nice... And in ways better than mine. however, isn't and "else" option required in the if statement in the end? (maybe that's just my habit though... if something goes wrong, then i give and "ERROR" message xD) RE: Password Generator - 3SidedSquare - 01-26-2014 You are indeed getting better! But there are a few things I really can't overlook; Critical: The script requires that it be saved with the name "password generator.py", because of this code: Code: if answer == "yes":
os.startfile("password generator.py")
close(password)Code: answer = 'yes'
while answer == 'yes':
length = random.randit(8,30)
...
answer = input("[PASSGEN] Would you like to generate a new password?")
close(password)Code: os.system('taskkill /PID %d /F' % os.getpid())Code: exit()Bad habits: It's good practice to only import the classes you need from a module EX: Code: from os import urandom, startfile, system, getpidRE: Password Generator - TheHelper - 01-26-2014 This looks pretty decent. Well done OP. RE: Password Generator - Equinox - 01-27-2014 (01-26-2014, 08:19 PM)cr33pyguy Wrote: I remember making one of these back a year... "elif" means else-if. I'm pretty sure it can function without an "else" but will shutdown if there is neither "yes" nor "no" in the input. (01-26-2014, 09:02 PM)3SidedSquare Wrote: You are indeed getting better! But there are a few things I really can't overlook; Thanks for the advice. However I run a Windows operating system and I don't really know much about other operating systems that run Python because it's different on Linux than it is on Windows. The exit is pretty useful, thanks for that. I seem to be putting useless lines of code that are useful yet can be done better. |