0

So far I have this program doing what i want. However when running through it will overwrite the last employee record instead of just adding to the file. I'm new to prgramming and have been staring at this for hours and i can't get it yet. Just need a little nudge in the right direction.

# Define Employee Class
# Common Base Class for all Employees
class EmployeeClass:
def Employee(fullName, age, salary):
    fullName = fullName
    age = age
    salary = salary
def displayEmployee():
    print("\n")
    print("Name: " + fullName)
    print("Age: " + age)
    print("Salary: " + salary)
    print("\n")

EmployeeArray = []

Continue = True
print ("Employee Information V2.0")

while Continue == True:
print ("Welcome to Employee Information")
print ("1: Add New Record")
print ("2: List Records")
print ("3: Quit")

choice = input("Pick an option: ")

if choice == "1":
    fullName = input ("Enter Full Name: ")
    if fullName == "":
        blankName = input ("Please enter a name or quit: ")
        if blankName == "quit":
            print ("Goodbye!")
            print ("Hope to see you again.")
            Continue = False
            break
    age = input ("Enter Age: ")
    salary = input ("Enter Salary: ")
    EmployeeRecords = open ('EmployeeRecords.txt' , 'w')
    EmployeeRecords.write("Full Name: " + fullName + '\n')
    EmployeeRecords.write("Age: " + age + '\n')
    EmployeeRecords.write("Salary: " + salary + '\n')
    EmployeeRecords.close()
elif choice == "2":
    EmployeeRecords = open ('EmployeeRecords.txt', 'r')
    data = EmployeeRecords.read()
    print ("\n")
    print (data)
    EmployeeRecords.close
elif choice == "3":
    answer = input ("Are you sure you want to quit? " "yes/no: ")
    if answer == "yes" or "y":
        print ("Bye!")
        Continue = False
    else:
        Continue
else:
    print ("Please choose a valid option")
    print ("\n")
NineTail
  • 223
  • 2
  • 15

2 Answers2

1

Append mode should work.

EmployeeRecords = open('EmployeeRecords.txt', 'a')
Danica
  • 28,423
  • 6
  • 90
  • 122
Kickaha
  • 3,680
  • 6
  • 38
  • 57
1

You are opening the file to be rewritten each time, based on the control string passed to open. Change open ('EmployeeRecords.txt, 'w')to open ('EmployeeRecords.txt', 'a+'). and the records will be appended to the end of the file.

shipr
  • 2,809
  • 1
  • 24
  • 32