I'm learning Python and I'm trying to combine saving text + specific element from the list to a txt file.
I managed to get the text out and it looks like this:
Project name: Main worker: Project name: Main worker:
and this is fine but I would like to add workers and projects that are in the lists to look like this:
Project name: Microsoft Main tester: Michael Scott Project name: Amazon Main tester: Dwight Schrute
And I'm not sure how to add that, this is what I got so far:
class Worker:
def __init__(self, name, lastname):
self.name = name
self.lastname = lastname
def name_lastname(self):
print(self.name, self.name)
class Project:
def __init__(self, name, worker):
self.name = name
self.worker = worker
workers = []
projects = []
name1 = input("Enter the name: ")
lastname1 = input("Enter the last name: ")
workers.append(name1 + ' ' + lastname1)
name2 = input("Enter the name: ")
lastname2 = input("Enter the last name: ")
workers.append(name2 + ' ' + lastname2)
projectname1 = input("Enter the name of the project: ")
projectname2 = input("Enter the name of the project: ")
projects.append(projectname1)
projects.append(projectname2)
print(workers)
print(projects)
file = open("projects.txt","w")
file.write("Project name: \n")
file.write("Main worker: \n")
file.write("Project name: \n")
file.write("Main worker: \n")
file.close()
If I understood you correctly, just writing the names and projects like you did with the headers (e.g. "Project name:") would be sufficient. For example:
file.write("Project name: \n")
file.write(projects[0] + "\n")
file.write("Main worker: \n")
file.write(workers[0] + "\n")
file.write("Project name: \n")
file.write(projects[1] + "\n")
file.write("Main worker: \n")
file.write(workers[1] + "\n")
file.close()
For lists, you can access the first element with a list[0], the second one with list[1] and so on. This is however a fairly manual approach and you could use a loop here to iterate over the list entries if you have more entries later on.