我可以/如何使用while&如何跳回特定的代码行;如果在一起?

发布于 2025-01-31 02:22:49 字数 1196 浏览 1 评论 0原文

我已经编写了我的代码以正常工作,但是如果用户选择尝试其他输出,我想跳过介绍性消息。例如,如果用户选择生成不同的密码,我希望返回第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文