I'm trying to write a code that outputs a username and a password
import string, random
def random_string(length=9, uppercase=True, lowercase=True, numbers=True):
character_set = ''
if uppercase:
character_set += string.ascii_uppercase
if lowercase:
character_set += string.ascii_lowercase
if numbers:
character_set += string.digits
return ''.join(random.choice(character_set) for i in range(length))
username = random_string(9)
for num in range(30):
password = f'{random.randint(100, 999)}{random.randint(100, 999)}{random.randint(100, 999)}'
print(f'{username}\n{password}')
This works on my end, but the problem I'm trying to solve is how do I let this run 30 times?
So I can get 30 different pairs of usernames and passwords.
If you want a collection of 30 random usernames and passwords, a list comprehension should serve nicely.
For instance:
[f'{random_string(9)}: {random.randint(100, 199)}' for _ in range(30)]
Obviously you can tweak this to generate the usernames and passwords howver you want, but it will give you a list of 30 of them that you can then use as you please.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.