我需要帮助我创建一个密码程序:
while len(pw) <= 6 and len(pw) >= 12 is True:
print ('')
else:
print ('Please Re-Enter your password, as it must be between 6-12 characters long')
continue
这是我当前的代码,但是continue在循环之外,所以我无法重新启动它。
那我怎么能重启循环呢?谢谢!
答案 0 :(得分:1)
试试这个:
pw = ''
while len(pw) < 6 or len(pw) > 12:
pw = raw_input('Please enter a password between 6-12 characters long: ')
运行如下:
>>> pw = ''
>>> while len(pw) < 6 or len(pw) > 12:
... pw = raw_input('Please enter a password between 6-12 characters long: ')
...
Please enter a password between 6-12 characters long: one
Please enter a password between 6-12 characters long: this is a long sentence
Please enter a password between 6-12 characters long: this works
>>>
在上面的代码中,我们将pw
设置为长度为0的字符串。然后,在长度符合我们的要求(6-12个字符)之前,我们要求输入。
或者,如果您想使用continue
:
>>> while True:
... pw = raw_input('Please enter a password between 6-12 characters long: ')
... if len(pw) < 6 or len(pw) > 12:
... print('Please Re-Enter your password, as it must be between 6-12 characters long')
... continue
... break
...
Please enter a password between 6-12 characters long: one
Please Re-Enter your password, as it must be between 6-12 characters long
Please enter a password between 6-12 characters long: this is a long sentence
Please Re-Enter your password, as it must be between 6-12 characters long
Please enter a password between 6-12 characters long: this works
>>>
你的continue
在while循环之外,你不能'重新进入',可以这么说。