Reputation: 87
I have an employee record, and It will ask them to enter their name and job and add both these elements to a tuple. I have done it so that it first adds to a list and then converts to a tuple.
However i want to print only the employee name not the job aswell.
I tried to make the final line print(mytuple[0])
but this doesn't work either.
record=[]
mytuple=()
choice = ""
while (choice != "c"):
print()
print("a. Add a new employee")
print("b. Display all employees")
choice = input("Choose an option")
if choice == "a":
full_name = str(input("Enter your name: ")).title()
record.append(full_name)
print(record)
job_title = str(input("Enter your job title: ")).title()
record.append(job_title)
print(record)
elif choice == "b":
print("Employee Name:")
for n in record:
mytuple=tuple(record)
print(mytuple)
Upvotes: 0
Views: 351
Reputation: 10891
You should make a list records
(useful name, it holds many records), and add a list
(we're calling this variable record
) for each employee.
records=[] # a new list, for all employees
# mytuple=() # you don't need this
choice = ""
while (choice != "c"):
print()
print("a. Add a new employee")
print("b. Display all employees")
choice = input("Choose an option")
if choice == "a":
record = list() # create new list for employee
full_name = str(input("Enter your name: ")).title()
record.append(full_name)
print(record)
job_title = str(input("Enter your job title: ")).title()
record.append(job_title)
print(record)
elif choice == "b":
print("Employee Name:")
for record in records:
print(record[0]) # record will be a list with first the name, then the title
print(record[1])
Upvotes: 0
Reputation: 1368
You're appending the full_name
and job_title
as separate entities into your record array. What you want is something like this when adding a new employee:
full_name = str(input("Enter your name: ")).title()
job_title = str(input("Enter your job title: ")).title()
record.append((full_name, job_title))
print(record[-1])
And then to display the name of all employees:
for name, _ in record:
print(name)
Upvotes: 0
Reputation: 1448
You should use dictionary if you want to access particular field name.In python list are just like array if you can fetch index sequence then you will be able to see your result.
but my suggestion use dictionary and then convert it into tuples. It will beneficial for you.
Upvotes: 0
Reputation: 399843
You seem to be iterating over a single record
, i.e. a list. It sounds as if you think you have a list of lists ("records"), but you never create that structure.
Obviously if you iterate over strings in a list, build a 1-element tuple from each, and then print it, you will end up printing all the strings in the list.
Upvotes: 1