我可以/如何使用while&如何跳回特定的代码行;如果在一起?
我已经编写了我的代码以正常工作,但是如果用户选择尝试其他输出,我想跳过介绍性消息。例如,如果用户选择生成不同的密码,我希望返回第11行(输入长度要求的请求),而不是第8行。(该行欢迎用户进入程序)。
import random
import string
import sys
another = True
while another:
8 print("Welcome to the Password Generator!")
# ask user for password length requirement
11 length = int(input("How many characters does your password require?: "))
# initialize character types
lower = string.ascii_lowercase
capitalize = string.ascii_uppercase
nums = string.digits
symbols = string.punctuation
# combine all char types
combine = lower + capitalize + nums + symbols
# randomize character types and add length requirement
randomization = random.sample(combine, length)
# construct the secure password
secure_pw = "".join(randomization)
# display password
print(secure_pw)
another = input("Would you like to generate a different secure password?: ")
while True:
if another == 'yes':
generate = True
break
elif another == 'no':
generate = False
sys.exit()
else:
another = input('Invalid response! Enter "yes" to continue or "no" to exit: ')
I've written my code to work fine, but if the user chooses to try a different output, I'd like to skip the introductory message. For example, if the user chooses to generate a different password, I'd prefer to return to line 11 (the request to enter a length requirement) rather than line 8. (which welcomes the user to the program).
import random
import string
import sys
another = True
while another:
8 print("Welcome to the Password Generator!")
# ask user for password length requirement
11 length = int(input("How many characters does your password require?: "))
# initialize character types
lower = string.ascii_lowercase
capitalize = string.ascii_uppercase
nums = string.digits
symbols = string.punctuation
# combine all char types
combine = lower + capitalize + nums + symbols
# randomize character types and add length requirement
randomization = random.sample(combine, length)
# construct the secure password
secure_pw = "".join(randomization)
# display password
print(secure_pw)
another = input("Would you like to generate a different secure password?: ")
while True:
if another == 'yes':
generate = True
break
elif another == 'no':
generate = False
sys.exit()
else:
another = input('Invalid response! Enter "yes" to continue or "no" to exit: ')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论