Python exercise 16 - Password Generator

Python exercise 16 - Password Generator

Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.

Extra:

Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

Code:

import random
import string

x=string.ascii_letters  # x='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
y=string.digits   # y='0123456789'
z=string.punctuation  # z='!#$%&()*+,-./:;<=>?@[\]^_`{|}~'

pswd_len=int(input('How many numbers you want to generate for your password? '))
pswd_select=str(input('Please tell you want weak password or strong. print W for weak, S for strong. ')) 

def generate_pswd (pswd_len,pswd_select):
	# if want weak password, generate a password with only letters, else mix with letters and numbers and punctuation
	if pswd_select =='W':
		return ''.join(random.sample(x,pswd_len))
		# return ''.join(random.choice(x) for _ in range(pswd_len))
	else:
		return ''.join(random.sample((x+y+z),pswd_len))

password = generate_pswd(pswd_len,pswd_select)
print (password)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值