0

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) 
1
  • Note that your problem is not to do with any difference between tuples and lists; for your purposes, they are almost completely identical, the only difference is that you can't append to a tuple. Commented Dec 2, 2015 at 10:28

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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) 

Comments

0

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]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.