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实现无聊的事情自动化”来自学
发布于 2022-06-05 22:49:48
您的程序看起来非常符合流程图,但是,为了完成它而不必显式调用exit()函数并在模块的末尾结束流,请考虑引入一个新的标志,我称之为of bol_access_denied.实际上,它可以用任何名字来称呼:
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.
希望能帮上忙。
https://stackoverflow.com/questions/72511229
复制相似问题