,直到用while循环输入一个有效的.我需要时间,但在哪里?



我需要用while循环修改程序,以不断提示用户输入密码。

def user_input():
""" takes input from user """
input1 = input("Enter a password : ")
if length_check(input1) != True or char_check(input1) != True:
exit()
elif length_check(input1) and char_check(input1):
input2 = input("Reenter password: ")
return input1, input2

def check_passwords(input1, input2):
"""
Compute and return the acceleration due to gravity. Normally this would be
a single line Docstring, like in function1, but I wanted to provide an
example of a multiline docstring. You can use these when a function needs
extra explanation.
"""
if input1 == input2:
print("Password changed.")
elif input2 != input1:
print("Password not changed.")

def length_check(input1):
if len(input1) <= 8:
print("Password too short. Minimum length is 8 characters.")
return False
else:
return True

def char_check(input1):
uppercase = []
numbers = list(range(0, 10))
for i in range(65, 91):
uppercase.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

def main():
""" Explain WHAT main() is doing """
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)
main()

user_input()函数更改为在输入无效时返回None(python中的NULL值)

def user_input():
""" takes input from user """
input1 = input("Enter a password : ")
if length_check(input1) != True or char_check(input1) != True:
# exit() # deleted
return None, None # added
elif length_check(input1) and char_check(input1):
input2 = input("Reenter password: ")
return input1, input2

main()函数更改为循环while输入无效(如果返回None,我们知道它无效)

def main():
""" Explain WHAT main() is doing """
input1, input2 = user_input()
while input1 == None and input2 == None: # added
input1, input2 = user_input() # added
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 # deleted

main()

我用# added注释了我添加的行,用# deleted注释了我从你的代码中删除的行。

相关内容

最新更新