【发布时间】:2021-11-11 04:52:50
【问题描述】:
编写一个程序,要求用户输入密码、名字、姓氏和 ID。如果其中任何一个无效,则程序会不断要求提供有效输入。规则如下:
- 密码必须至少为 7 个字符,必须包含至少一个大写字母、至少一个小写字母和至少 1 个数字
- 名字和姓氏只能包含字母
- ID 只能包含数字
在您输入所有内容后,将它们有效地打印在屏幕上。由于密码是敏感信息,只打印前三个字符,其余长度,打印'*'
password = 'Pass1234'
first_name = 'John'
last_name = 'Smith'
ID = '1234'
p = input('Input your password: ')
if (len(p)<6):
print('Invalid password. Must be at least 6 characters')
p = input('Input your password: ')
elif not p.islower():
print('Invalid password. Must have at least 1 lowercase character')
elif not p.isupper():
print('Invalid password. Must have at least 1 uppercase character')
if p == password:
print('Well done! This is a valid password')
password_list = list(password)
password_list[3] = '*'
password_list[4] = '*'
password_list[5] = '*'
password_list[6] = '*'
password_list[7] = '*'
edited_password = ''.join(password_list)
fn = input('Please enter your first name: ')
ln = input('Please enter your last name: ')
for f in fn:
if f.isdigit():
print('Invalid Name! Name should only contain letters')
fn = input('Please enter your first name: ')
for l in ln:
if l.isdigit():
print('Invalid Name! Name should only contain letters')
ln = input('Please enter your last name: ')
if fn == first_name or 'john' and ln == last_name or 'smith':
print('Well done! This is a valid name')
I = input('Please enter your ID: ')
if I.isupper():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I.islower():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I == ID:
('Well done! This is a valid ID')
print('Name:', first_name, last_name)
print('ID:', ID)
print('Password:', edited_password)
输出:
Input your password: Pass1234
Invalid password. Must have at least 1 lowercase character
Well done! This is a valid password
Please enter your first name: John
Please enter your last name: Smith
Well done! This is a valid name
Please enter your ID: 1234
Name: John Smith
ID: 1234
Password: Pas*****
如何修复我的程序不打印小写错误消息?
【问题讨论】:
标签: python python-3.x