在循环中创建多个 if 语句并打印到文本文件
Creating multiple if statements within a loop and printing to a text file
我正在尝试创建一个包含多个 if,else 语句的循环,然后将结果打印到文本文件中。我还想将两个单独的类别写入文本文件,一个用于 'staff',一个用于 'teaching staff'。请问有谁能协助实现这个功能吗?下面是我的代码副本。
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()
与其只使用一个文件来写入它们,不如考虑定义一个新文件来将新输入存储到它们各自的类别中。
此外,您在 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
我正在尝试创建一个包含多个 if,else 语句的循环,然后将结果打印到文本文件中。我还想将两个单独的类别写入文本文件,一个用于 'staff',一个用于 'teaching staff'。请问有谁能协助实现这个功能吗?下面是我的代码副本。
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()
与其只使用一个文件来写入它们,不如考虑定义一个新文件来将新输入存储到它们各自的类别中。
此外,您在 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