繁体   English   中英

打印语句无限打印

[英]Print statement printing infinitely

password=input("Please enter your chosen password within 8 and 24 letters: ")
while len(password)>8 and len(password)<24:
    print("this password is within the given length range")
else:
    password=input("Please enter a password within the boundaries: ")

当我运行代码并且输入的密码长度超过8和24时,它只是无限打印“此密码在给定的长度范围内”。 我不擅长编码,我确定我做错了什么。

您忘记了用于停止循环的break语句。 并且循环语句有问题,主要是您缺少elseif部分。

password=input("Please enter your chosen password within 8 and 24 letters: ")
while True:                                   #will continue until break statement executes
    if len(password)>8 and len(password)<24:
        print("this password is within the given length range")
        break                                                                #Quit the loop
    else:
        password=input("Please enter a password within the boundaries: ")

上面的代码将一直运行,直到用户输入密码8 < length < 24

如果你想不断地提示他们输入密码,你需要像这样在你的 while 循环中输入你的提示,并改变周围的小于和大于符号。

password = ""
while len(password) < 8 or len(password) > 24:
    password = input("Please enter your chosen password within 8 and 24 letters: ")

else仅在您使用break退出while循环后才执行(与条件变为假时相反)。 你只是想要

password=input("Please enter your chosen password within 8 and 24 letters: ")
while len(password) < 8 or len(password) > 24:
    password=input("Please enter a password within the boundaries: ")

如果您不介意对两个输入使用相同的提示,请使用带有显式中断的无限循环:

while True:
    password=input("Please enter your chosen password within 8 and 24 letters: ")
    if 8 <= len(password) <= 24:
        break

您将有效密码存储在“密码”变量中。 while 循环检查 'password' 是否有效,确认它是有效的,然后继续运行。 如果用户输入无效密码而不是有效密码,您希望循环继续进行。 尝试:

password=input("Please enter your chosen password within 8 and 24 letters: ")
while len(password)<8 or len(password)>24:
    password=input("Please enter a password within the boundaries: ")     

print("this password is within the given length range")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM