【发布时间】:2017-09-01 20:23:08
【问题描述】:
当我运行代码时,我的 while 循环运行良好,然后程序就结束了,即使我有一个完整的其他函数想要运行。 在这段代码之前有一个 while 循环,它以 login = False 结尾,我也尝试用 break 替换它,同样的事情发生了。
import csv
import sys
username="Leeman"
password="treeroad"
login = True
login_u = input("Enter username ")
login_p = input("Enter password ")
while login == True:
if login_u + login_p != username + password:
print("incorrect login")
sys.exit()
elif login_u != username:
print("incorrect login")
sys.exit()
elif login_p != password:
print ("incorrect login")
sys.exit()
elif login_u + login_p == username + password:
print("Welcome to the system")
login = False
def main_menu():
print("---------------------------------School Menu-----------------------------------")
option=input("""Options:
1-Enter new student details
2-Search for student by ID number
3-View student details
4-Reports
5-Logout
Where do you want to go, 1,2,3,4 or 5?
""")
if option == "1":
details=input("Enter your new student's details in format:ID Number,Forename,Surname,Gender,Tutor Group,DOB(dd/mm/yyyy),Phone Number,School Email: ")
appendfile=open('classinfo.csv ' , 'a')
appendfile.write(details)
appendfile.close
main_menu()
elif option=='2':
with open ('classinfo.csv' , 'r') as classinfoFile:
idnumber = input("Input the ID number of the student you wish to view")
classinfoReader = csv.reader(classinfoFile)
for row in classinfoReader:
for field in row:
if field == idnumber:
print (row)
main_menu()
此代码的目的是您通过选择一个数字 (1-5) 来选择您想要对 csv 文件执行的操作,然后在您完成后返回学校菜单。但是整个功能根本没有运行。为什么?
【问题讨论】:
-
请重新排列您的代码
-
你想怎么排列?
-
你的缩进看起来很混乱。
-
欢迎来到 Stack Overflow!请查看我们的"How to Ask" 页面。正如该页面所说,我们很乐意为您提供帮助,但如果没有minimal, complete, and verifiable example,我们将无法做到这一点。
-
另外,在某些时候,您必须实际调用您的函数才能运行。