简体   繁体   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: ")

When I run the code and the length of the entered password is more than 8 and 24, it just prints "This password is within the given length range" infinitely.当我运行代码并且输入的密码长度超过8和24时,它只是无限打印“此密码在给定的长度范围内”。 Im not good at coding and im sure ive done something wrong.我不擅长编码,我确定我做错了什么。

You forgot the break statement to stop the loop.您忘记了用于停止循环的break语句。 And the loop statement has problems, mainly that you are missing the if part of the else .并且循环语句有问题,主要是您缺少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: ")

The above code will run until the user enters a password of 8 < length < 24 .上面的代码将一直运行,直到用户输入密码8 < length < 24

If you want to continually prompt them for a password you'll need your prompt inside your while loop like this and change the less than and greater than signs around.如果你想不断地提示他们输入密码,你需要像这样在你的 while 循环中输入你的提示,并改变周围的小于和大于符号。

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

The else only executes after you exit the while loop using break (as opposed to when the condition becomes false). else仅在您使用break退出while循环后才执行(与条件变为假时相反)。 You simply want你只是想要

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: ")

If you don't mind using the same prompt for both inputs, use an infinite loop with an explicit break:如果您不介意对两个输入使用相同的提示,请使用带有显式中断的无限循环:

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

You store a valid password in the 'password' variable.您将有效密码存储在“密码”变量中。 The while loop checks if 'password' is valid, confirms that it is, and it keeps on going. while 循环检查 'password' 是否有效,确认它是有效的,然后继续运行。 You want your loop to keep on going if the user type an invalid password, rather than a valid one.如果用户输入无效密码而不是有效密码,您希望循环继续进行。 Try:尝试:

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