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 loops正確繪制流程圖。我正在通過“用 Python 自動化無聊的事情”來自學
uj5u.com熱心網友回復:
您的程式看起來非常符合您的流程圖,但是,要完成它而不必顯式呼叫 exit() 函式并在模塊末尾結束流程,請考慮引入一個新標志,我稱之為我的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://www.uj5u.com/qianduan/486709.html
上一篇:如何在sed中編輯多行
下一篇:如何從字串中寫出一個單詞。C#
