1

How do I print only 1 copy of data? I am a complete beginner. This is Python code I did for Add data.

This is my current output:

Row | Name | Age | Grade
1     jo     23     1
2     jo     23     1
1     ki     24     2
2     ki     24     2

This is my expected output:

Row | Name | Age | Grade
1     jo     23     1
2     ki     24     2

Here is my code:

name = []
age = []
grade = []
record = []
rowCount = 0
choice = 1
while int(choice) != 0:
    if int(choice) > 4 or int(choice) < 1:
        choice = input ("Invalid input. Enter choice: ")
    else:
        print("Main Menu:")
        print("1- Add")
        print("2- Delete")
        print("3- Edit")
        print("4- Print Report")
        print("0- End Program")
        choice = input ("Enter choice: ")
        if int(choice) == 1: #Add function
            name.append(input("Input Name: "))
            age.append(input("Input Age: "))
            grade.append(input("Input Grade: "))
            print ("Row | Name | Age | Grade")
            for x, y, z in zip(name,age,grade):
                for w in range(len(name)):
                    print(str(w+1)  + "     " + x + "     " + y + "     " + z)
                    w+1
        elif int(choice) == 2: #Delete function
            print("Delete func")
        elif int(choice) == 3: #Update function
            print("Edit func")
        elif int(choice) == 4: #Print function
            print("Print func")
print("Ending Program...")
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Wana Anda
  • 13
  • 2

2 Answers2

0

For each entry in zip(name, age, grade), you have another nested loop (for w). This causes the program to print multiple rows per entry. One way to solve it is to include w inside the zip; i.e., replacing

for x, y, z in zip(name,age,grade):
    for w in range(len(name)):
        print(str(w+1)  + "     " + x + "     " + y + "     " + z)
        w+1

with

for x, y, z, w in zip(name, age, grade, range(len(name))):
    print(str(w+1)  + "     " + x + "     " + y + "     " + z)
j1-lee
  • 13,764
  • 3
  • 14
  • 26
0

You could use a list of lists for each group of data, then use set function

For example:

students = []
...
while True:
   name = input("Input Name: ")
   age = input("Input Age: ")
   grade = input("Input Grade: ")
   student.append([name, age, grade])
   

Then, to get only the unique data on your list of lists, you can use set, but set doesn't work on list of lists, so you can iterate though it and treat each list as a tuple, then use set on the tuples, which I adapted from this answer i.e.:

unique_students = set(tuple(row) for row in (students)))

Now you have a new variable with the unique data from students and you can iterate though it and print the data as you wish.

Gabriel Caldas
  • 371
  • 2
  • 13
  • Sorry, I only realized now that you problem was the duplicated data being printed. Anyways, I'm gonna leave my answer here and hope it may be useful for someone someday. – Gabriel Caldas Jun 13 '21 at 02:44