2017-09-01 61 views
-3

当我运行代码时,我的while循环完美地工作,然后程序就结束了,即使我有一个我想要运行的其他函数。 在以login = False结尾的这段代码之前有一段while循环,我也尝试用break来代替它,并且发生同样的事情。python-当我运行我的函数时没有任何事情发生

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文件到你已经做到了回到学校菜单,然后经过什么。然而,整个功能根本没有运行。为什么?

+0

重新安排你的代码,请 –

+0

重新安排它以何种方式,你想? – user8549523

+0

您的缩进看起来很混乱。 –

回答

0
def do_one(): 
    print("You entered 1!") 

def do_two(): 
    print("You entered 2!") 

def main(): 
    while True: 
     choice = int(input("Enter 1, 2, or 3 (to exit): ")) 
     if choice == 1: 
      do_one() 
     elif choice == 2: 
      do_two() 
     elif choice == 3: 
      print("Goodbye!") 
      break 

main() 
+0

我已经将它放在程序中看看会发生什么,当我运行它时,会发生同样的事情,只要我输入'def'之后就会发生什么,只是根本没有运行。 – user8549523

+0

嗯......这是一个如何构建你的结构的例子程序。 'def'创建一个新函数但不运行它。代码由最后一行运行,它调用'main()'。在main()里面你有一个while循环,它会永远重复(或者至少在你到达break之前)。 –

相关问题