Random Generator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Random Generator

Guys, I need your help! I need to create a program that can generate a password in python but that only contain lowercase letters and numbers, with a length that is decided by the user. How can I do this? Thx

30th Mar 2021, 6:01 PM
Stefano De Angelis
Stefano De Angelis - avatar
7 Answers
+ 2
import string import random up = string.ascii_uppercase low = string.ascii_lowercase nums = string.digits # you can use punctuation as well # punc = string.punctuation all = up + low + nums # + punc length = input("how many characters? ") # password = "" # you can use for loop #for _ in range(int(length)): # password +=random.choice(all) # but I prefer the fastest method password = "".join(random.sample(all, int(length))) print(password)
31st Mar 2021, 3:48 AM
iTech
iTech - avatar
+ 1
I don't know realy but i think a madul does exist
30th Mar 2021, 6:13 PM
MohammadMahdi
MohammadMahdi - avatar
+ 1
U can search on web probably u can find this Especially pyp maduls
30th Mar 2021, 6:15 PM
MohammadMahdi
MohammadMahdi - avatar
0
You can create a string containing the alphabet and numbers, and then extract an index from it using a random class, and you can use an input for the number desired by the user, and continue with the loop until you reach the desired number of characters. Good luck
30th Mar 2021, 6:08 PM
MohammadMahdi
MohammadMahdi - avatar
0
Yeah, but there is a built-in function in python? Only for making the code a bit shorter. Thx
30th Mar 2021, 6:11 PM
Stefano De Angelis
Stefano De Angelis - avatar
0
You need string.ascii_lowercase, string.digits [https://docs.python.org/3/library/string.html], concatenate them and use secrets.choice [https://docs.python.org/3/library/secrets.html?highlight=random%20choice#secrets.choice] to select random elements
30th Mar 2021, 6:52 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
30th Mar 2021, 7:07 PM
Stefano De Angelis
Stefano De Angelis - avatar