Home > OS >  Putting all information of list in another list
Putting all information of list in another list

Time:07-30

Hi guys I'm new in programming and Stackoverflow and English is my second language if I have some mistakes , forgive me. I'm writing code for my teacher about a list which you get information name , ID number , and name. My problem is that at the end of loop I don't know how to make a list of All information . that's my code :

loopnumber = int(input("please enter your number of people"))
for i in range (loopnumber) :

    IDnumber = input("please enter your ID number.")
    while len(IDnumber) != 10 :
        IDnumber = input("please try again.")

    name = input("please enter your name.")

    age = int(input("please enter your age."))
    while age <= 0 :
        age = int(input(" please try Again."))

    listpeople = [IDnumber, name, age ]

Thanks a lot.

CodePudding user response:

listpeople = [] 
loopnumber = int(input("please enter your number of people"))
for i in range (loopnumber) :

    IDnumber = input("please enter your ID number.")
    while len(IDnumber) != 10 :
        IDnumber = input("please try again.")

    name = input("please enter your name.")

    age = int(input("please enter your age."))
    while age <= 0 :
        age = int(input(" please try Again."))

    personal_details = [IDnumber, name, age] # Rename this variable to personal_details or personal_info because it holds each person's information 
    listpeople.append(personal_details) 

CodePudding user response:

Create an empty list of people

listPeople = []

At the end of the for loop, append the list of IDnumber, name and age to the listPeople list

listPeople = []

loopnumber = int(input("please enter your number of people: "))

for i in range(loopnumber):
    IDnumber = input("please enter your ID number: ")
    while len(IDnumber) != 10:
        IDnumber = input("please try again: ")

    name = input("please enter your name: ")

    age = int(input("please enter your age: "))
    while age <= 0:
        age = int(input(" please try Again: "))

    listPeople.append([IDnumber, name, age])

print(listPeople)

Output:

please enter your number of people: 2
please enter your ID number: 1111111111
please enter your name: geeky
please enter your age: 19
please enter your ID number: 2222222222
please enter your name: quentin
please enter your age: 19
[['1111111111', 'geeky', 19], ['2222222222', 'quentin', 19]]

CodePudding user response:

Start with a list then keep appending your information as a list itself.

# start with the empty list
listpeople = []

loopnumber = int(input("please enter your number of people"))

for i in range (loopnumber) :
    IDnumber = input("please enter your ID number.")
    while len(IDnumber) != 10 :
        IDnumber = input("please try again.")

    name = input("please enter your name.")

    age = int(input("please enter your age."))
    while age <= 0 :
        age = int(input(" please try Again."))

    # put all the data you have in a list
    individual_details = [IDnumber, name, age]

    # append the personal_details list to the parent list aka listpeople
    listpeople.append(individual_details)

A diagram of the process

  • Related