我正在嘗試使用多個 if,else 陳述句創建一個回圈,然后將結果列印到文本檔案中。我還想在文本檔案中寫入兩個單獨的類別,一個用于“員工”,一個用于“教職員工”。有沒有人可以幫助實作這個功能?我的代碼副本如下。
staffType = input("Enter 1 for Staff, 2 for Teaching staff or enter 0 to exit: ")
if staffType=='1':
firstInput = input("Enter first name: ")
lastInput =input("Enter last name: ")
depInput = input("Enter department name: ")
staffType = input("Is a staff or a teacher? ")
elif staffType=='2':
firstInput = input("Enter first name: ")
lastInput =input("Enter last name: ")
depInput = input("Enter department name: ")
staffType = input("Is a staff or a teacher? ")
disciplineInput = input("Enter staff member's discipline: ")
lnInput = input("Enter staff member's license number: ")
if StaffType == "0":
print("You have exited the program")
break
StaffFile = open("staff.txt", "a")
StaffFile.write('\nThe staff members are:\n' dobInput, firstInput, lastInput, staffType '\n')
StaffFile.write('\nThe teaching staff are:\n' dobInput, firstInput, lastInput, staffType, disciplineInput, lnInput '\n')
StaffFile.close()
uj5u.com熱心網友回復:
不要只使用一個檔案來寫入它們,而是考慮定義一個新檔案來將新輸入存盤到它們各自的類別中。
另外,您在write
staffType = input("Enter 1 for Staff, 2 for Teaching staff or enter 0 to exit: ")
if staffType=='1':
firstInput = input("Enter first name: ")
lastInput =input("Enter last name: ")
depInput = input("Enter department name: ")
staffType = input("Is a staff or a teacher? ")
StaffFile = open("staff.txt", "a")
StaffFile.write('\nThe staff members are:\n' firstInput s lastInput s staffType '\n')
StaffFile.close()
elif staffType=='2':
firstInput = input("Enter first name: ")
lastInput =input("Enter last name: ")
depInput = input("Enter department name: ")
staffType = input("Is a staff or a teacher? ")
disciplineInput = input("Enter staff member's discipline: ")
lnInput = input("Enter staff member's license number: ")
StaffFile = open("teaching_staff.txt", "a")
StaffFile.write('\nThe teaching staff are:\n' firstInput s lastInput s staffType s disciplineInput s lnInput '\n')
StaffFile.close()
if StaffType == "0":
print("You have exited the program")
break
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457670.html
