Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading