'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.
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 1:[1]
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'+ 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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
