【问题标题】:While Loop in Python ( Beginner) [closed]Python中的while循环(初学者)[关闭]
【发布时间】:2020-09-04 04:57:47
【问题描述】:

为此而苦苦挣扎。只是一个初学者,所以对我来说放轻松! :) 尝试让用户输入密码,如果不在 6-10 个字符之内。他们被要求再次输入。 当密码正确时,循环停止。 即使输入了正确的长度密码,我似乎也无法让它退出循环。!

min_password_lenght = 6
max_password_lenght = 10

password = input(“enter password:”)
password_lenght = len(password)

while password_lenght > 6 or password_lenght < 10:
          print(“error”)
          password = input(“enter again:”)

print (“password correct”)

【问题讨论】:

  • 您需要将代码的文本版本粘贴到问题的正文中,该图像几乎无法阅读,因此不推荐使用此方式提出问题。
  • 你没有更新password_length并检查错误的范围。

标签: python while-loop passwords


【解决方案1】:

您下次可能应该更具体,但这里有一个满足您描述的 sn-p:

pwd = ""
while len(pwd) < 6 or len(pwd) > 10:
    pwd = input("Enter password: ")

【讨论】:

  • 或者如果您不想计算两次长度:while not 6 &lt;= len(pwd) &lt;= 10:
猜你喜欢
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2015-07-14
  • 2013-09-26
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多