首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >试图理解流程控制/图表绘制

试图理解流程控制/图表绘制
EN

Stack Overflow用户
提问于 2022-06-05 21:25:11
回答 1查看 63关注 0票数 1
代码语言:javascript
运行
复制
import sys

access = False
while not access:
    username = input('Enter username: ')
    if username.lower() != 'joe':
        print("imposter!")
        continue
    
    else:
        print(f'Hello {username.capitalize()}')
        for i in range(3):
            password = input('Enter password: ')
            if password == 'Water':
                access = True
                break
        else:
            print("3 strikes, you're out")
            sys.exit()

print("Access granted")

这是正确的代码流程图吗?我正在尝试理解如何正确地用for循环绘制流程图。我正在通过“用Python实现无聊的事情自动化”来自学

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-05 22:49:48

您的程序看起来非常符合流程图,但是,为了完成它而不必显式调用exit()函数并在模块的末尾结束流,请考虑引入一个新的标志,我称之为of bol_access_denied.实际上,它可以用任何名字来称呼:

代码语言:javascript
运行
复制
import sys

bol_access_denied = False  # You will need to introduce a flag before you enter your while...loop.
access = False
while not access:
    username = input('Enter username: ')
    if username.lower() != 'joe':
        print("imposter!")
        continue
    
    # else:  # This else can be omitted since program flows here ONLY when username == joe 
    print(f'Hello {username.capitalize()}')
    for i in range(3):
        password = input('Enter password: ')
        if password == 'Water':
            access = True
            break
    else:
        print("3 strikes, you're out")
        bol_access_denied = True  # set the flag here.
        # sys.exit()  This can be replaced by 'break', then backed by [see STEP Final-Check]
    if bol_access_denied:  # Test the flag here.
        break

if access:  # STEP Final-Check, is to navigate the user to a personalized section of your program.
    print("Access granted")

# Your program ends here naturally without explicitly invoking the exit function.

希望能帮上忙。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72511229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档