【发布时间】:2021-05-28 13:33:23
【问题描述】:
我需要使用 while 循环修改程序以不断提示用户输入密码。 定义用户输入(): """ 从用户 """ 获取输入 input1 = input("输入密码:") 如果 length_check(input1) != True 或 char_check(input1) != True: 出口() elif length_check(input1) 和 char_check(input1): input2 = input("重新输入密码:")
return input1, input2
def check_passwords(input1, input2): """ 计算并返回重力加速度。通常这将是 单行 Docstring,就像在 function1 中一样,但我想提供一个 多行文档字符串的示例。您可以在功能需要时使用这些 额外的解释。 """ 如果输入 1 == 输入 2: print("密码已更改。") elif 输入 2 != 输入 1: print("密码未更改。")
def 长度检查(输入 1): 如果 len(input1)
def char_check(input1): 大写 = [] 数字 = 列表(范围(0, 10)) 对于范围内的 i (65, 91): 大写.append(chr(i))
counter = 0
for i in input1: # Batman Surfs
if i in uppercase:
counter += 1
# print(counter)
if counter >= 2:
if not any(char.isdigit() for char in input1):
print('Password should have at least one numeral')
return False
else:
return True
# for i in input1:#Batman Surfs 1
# if i in numbers:
# return True
# else:
# print("Password must contain at least one number.")
# return False
else:
print("Password must contain at least two uppercase letters.")
return False
定义主(): """ 解释 main() 在做什么"""
input1, input2 = user_input()
char_check(input1)
check_passwords(input1, input2)
# function1(12, 13)
# m_e = 5 # mass in kg
# r_e = 6 # radius in metres
# gravity_on_earth = function2(m_e, r_e)
# print(gravity_on_earth)
enter code here
main()
【问题讨论】:
-
只需将第 5 行从
exit()更改为return user_input()。不完全使用while,而是使用递归 -
你的答案很棒但是老师希望我们使用 while :
-
修改程序功能,不断提示用户输入密码,直到输入有效密码。您的解决方案必须使用 while 循环。前面部分的功能应该保留。输入新密码:batman 密码太短。最小长度为 8 个字符。输入新密码:batman rocks 密码必须至少包含两个大写字符。输入新密码:Batman Rocks 密码必须包含至少一个数字。输入新密码:Batman Rocks 1 重新输入密码:Batman Rocks 1 密码已更改。
-
编辑您的问题以添加这些详细信息,而不是将其作为评论发布
标签: python while-loop