【问题标题】:How can I make the program go to the input?如何让程序进入输入?
【发布时间】: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


【解决方案1】:

你可以做这样的事情。您遇到的问题是您正在检查您的整个密码是否为小写,而不是至少有 1 个字符。

当然,您可能希望将输入变量从“p”更改为“密码”,而不是在页面顶部对其进行硬编码。

p = input("Input your password: ")
upper = 0
lower = 0 
number = 0
while True:
    for x in p:
        if x.isupper()==True:
            upper+=1
        elif x.islower()==True:
            lower+=1
        elif x.isalpha()==False:
            number+=1
    if len(p) < 7 or upper <= 0 or lower <= 0 or number <= 0:
        print ('password not complex enough')
        p = input('please enter password again ')
        upper = 0
        lower = 0
        number = 0
    else:
        break

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2019-10-30
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多