Home > Net >  Creating multiple if statements within a loop and printing to a text file
Creating multiple if statements within a loop and printing to a text file

Time:04-08

I am trying to create a loop with multiple if,else statements which will then print the results to a text file. I would also like to write two separate categories to the text file, one for 'staff' and one for 'teaching staff'. Is anyone able to assist with making this functional please? A copy of my code is below. Thank you!

    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()

CodePudding user response:

instead of using only one file to write them on, consider defining a new file to store new inputs to their respective category.

also, you were missing some commas at the 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', dobInput, firstInput, lastInput, 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', dobInput, firstInput, lastInput, staffType, disciplineInput, lnInput '\n')
        StaffFile.close()

if StaffType == "0":
        print("You have exited the program")
        break
  • Related