2017-09-01 58 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之前)。 –

相關問題