Append Values from input to a sublist in Python
<p>I am trying to append values from an input to sublists in a list.
Each number of Student and name should be in a sublist.
ex: </p>

<pre><code>[[123,John],[124,Andrew]]
</code></pre>

<p>Where the outside list would be the number of students, and the sublists , the info of the students..</p>

<p>Here is what my code looks like:</p>

<pre><code>listStudents = [[] for _ in range(3)]
infoStudent = [[]]

while True:
    choice = int(input("1- Register Student 0- Exit"))
    cont = 0
    if choice == 1:
            snumber = str(input("Student number: "))
            infoStudent[cont].append(str(snumber))
            name = str(input("Name : "))
            infoStudent[cont].append(str(name))
            cont+=1
            listStudents.append(infoStudent)
    if choice == 0:
        print("END")
        break


print(listStudents)

print(infoStudent)
</code></pre>

<p>If I put on the first loop, <code>snumber = 123</code> , <code>name = john</code> , and <code>snumber = 124</code>, <code>name = andrew</code> on the second time it will show me : <code>[[123,john,124,andrew]]</code> instead of <code>[[123,john], [124,andrew]]</code>.</p>

<p>Your code can be more pythonic and can make use of some basic error handling as well. Create the inner list inside the while loop and simply append to the outer student list. This should work.</p>

<pre><code>students = []
while True:
    try:
        choice = int(input("1- Register Student 0- Exit"))
    except ValueError:
        print("Invalid Option Entered")
        continue

    if choice not in (1, 9):
        print("Invalid Option Entered")
        continue

    if choice == 1:
        snumber = str(input("Student number: "))
        name = str(input("Name : "))
        students.append([snumber, name])
    elif choice == 0:
        print("END")
        break

print(students)
</code></pre>

python list append sublist