【问题标题】:until a valid one is entered with while loop.I need a while but where?直到使用 while 循环输入有效的循环。我需要一段时间,但在哪里?
【发布时间】: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


【解决方案1】:

当输入无效时,更改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 注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多