我可以在函数中分别运行两个while循环吗?如果不是,我如何在python中验证列表



我可以在函数中分别运行两个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列表?

您可以在函数内一个循环后运行一个循环,甚至可以在python中一个循环之后运行一个环路。我读了你的代码,可能有一个逻辑错误。表达式continue在STUDENT_ID中找到sid之后工作。因此,循环再次运行。除非我误解了你想要什么,否则你应该在第二个循环中用break替换continue

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 foundn")
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")

第二个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")

相关内容

最新更新