概括
我已經構建了一個燒瓶應用程式,該應用程式從用戶那里獲取了幾個輸入,并且這些輸入需要根據某些標準進行驗證。如果輸入未正確驗證,則會顯示一條閃爍訊息,說明輸入的錯誤之處。
主頁有 4 個嵌套的 if 陳述句,它們根據正確的結果顯示某些訊息。
Python代碼
if request.method == 'POST':
VM = request.form['email']
RM = request.form['mobile']
if isEmailInUse(VM):
if VM not in blacklist and VM.endswith("@gmail.com"):
if len(RM) == 11 and RM.startswith("555"):
create(user)
return redirect(url_for('see_users'))
else:
flash('Invalid mobile! ', 'error')
else:
flash('Invalid Email Address! ', 'error')
else:
flash('Email Address already in use. ', 'error')
return render_template('Validate_Email.html')
我努力了
if VM not in blacklist and VM.endswith("@gmail.com"): flash('Invalid Email Address! ', 'error')
但它繼續使用代碼。
有沒有一種替代方法可以在燒瓶中使用這么多嵌套的 if 來驗證用戶輸入的燒瓶訊息?
一些指導或幫助將不勝感激!
uj5u.com熱心網友回復:
根據需要顛倒測驗的順序并否定邏輯,例如:
if request.method == 'POST':
VM = request.form['email']
RM = request.form['mobile']
if not checkinUse(VM):
flash('Email Address already in use. ', 'error')
elif if VM in blacklist or not VM.endswith("@gmail.com"):
flash('Invalid Email Address! ', 'error')
elif len(RM) != 11 or not RM.startswith("555")
flash('Invalid mobile! ', 'error')
else:
create(user)
return redirect(url_for('see_users'))
return render_template('Validate_Email.html')
如果電子郵件地址未使用,則PScheckinUse似乎是一個回傳 True 的函式的名稱選擇有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425133.html
上一篇:在發布模式下編譯Xamarin應用程式時出錯:System.InvalidOperationException:序列不包含任何元素
