我可以在函式中分別運行兩個while回圈嗎?如果不是,我如何驗證 python 中的串列?
def ADD_RESULT():
while True:
code = input("Enter the code")
if code in COURSE_CODE :
print("Details found")
break
else:
print("course didn't match")
continue
print("")
while True:
sid = input("Enter student id")
if sid in STUDENT_ID:
print(" found")
continue
else:
print(" not found")
break
如何使用 while 回圈來驗證上面的 python 串列?
uj5u.com熱心網友回復:
您可以在函式內的回圈之后運行回圈,更好的是您可以在 python 中的回圈之后運行回圈。我看了你的代碼,可能有邏輯錯誤。在 STUDENT_ID 中找到該運算式continue后起作用sid。因此回圈再次運行。除非我誤解了您想要的內容,否則您應該在第二個回圈中替換continue為break。
while True:
sid = input("Enter student id")
if sid in STUDENT_ID:
print(" found")
break
else:
print(" not found")
此外,您不必continue在回圈結束時使用。因此continue第一個回圈中的陳述句是不必要的。
經過上述修改后,代碼如下所示:
def ADD_RESULT():
while True:
code = input("Enter the code: ")
if code in COURSE_CODE:
print("Details found\n")
break
else:
print("course didn't match")
while True:
sid = input("Enter student id: ")
if sid in STUDENT_ID:
print(" found")
break
else:
print(" not found")
uj5u.com熱心網友回復:
似乎您在第二個while回圈中的回圈邏輯與第一個回圈中的不同。嘗試替換 break 陳述句的位置。此外,此處不需要繼續。
def ADD_RESULT():
while True:
code = input("Enter the code")
if code in COURSE_CODE :
print("Details found")
break
else:
print("course didn't match")
print("")
while True:
sid = input("Enter student id")
if sid in STUDENT_ID:
print(" found")
break
else:
print(" not found")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407302.html
標籤:
下一篇:使用兩個串列python創建表
