0

Hi am new just started Computer Science at London Met we have programming in Python. In this code am trying to add items to already existing appended list list.append([student_name, student_age, student_sex]) from user input student_grade. How can i do this for each item in range. SO next time i print student_grade will be added to at the end of statement in form {each_item[3]} and 3 in the case will be student_grade ?

thanks in advance

from io import StringIO
import sys

number_student = int(input("How many students You want to add: "))

list = []
for i in range(0, number_student):
student_name = input("Whats Your name student? ")
student_age = input("Whats Your age student? ")
student_sex = input("Female or male? ")
list.append([student_name, student_age, student_sex])

comment withhash  student_1 = (student_name, student_age + "years old", student_sex)

for each_item in list:
print(f"Student name is {each_item[0]}, you are {each_item[1]} years old and you are {each_item[2]}")

student_grade = input("Add student grade: ")
list.extend(["student_grade"])
#list.extend([student_grade])

for each_item in list:
print(f"Student name is {each_item[0]}, you are {each_item[1]} years old and you are {each_item[2]}, and your grades are", student_grade)
New contributor
kr3m213 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • 1
    Please use punctuation in your questions' explanation and indentation in the code. 14 hours ago
  • What is comment withhash student_1? You never use student_1 anywhere.
    – Barmar
    14 hours ago
  • @bertwassink He's a victim of the broken Stacks editor that was deployed this week.
    – Barmar
    14 hours ago
  • You shouldn't be using list.extend(). You need to use each_item.append()
    – Barmar
    14 hours ago
  • @barmar Ah, I hope that the Stacks editor will be fixed then. 14 hours ago

1 Answer 1

0

You shouldn't be adding student_grade to list, you should be adding it to each_item, the list for the current student.

Then when printing the grade, you use each_item[3] to get it.

Also, don't use list as a variable name, because it's a built-in function. I've renamed it to student_list below.

number_student = int(input("How many students You want to add: "))

student_list = []
for i in range(0, number_student):
    student_name = input("Whats Your name student? ")
    student_age = input("Whats Your age student? ")
    student_sex = input("Female or male? ")
    student_list.append([student_name, student_age, student_sex])

#student_1 = (student_name, student_age + "years old", student_sex)

for each_item in student_list:
    print(f"Student name is {each_item[0]}, you are {each_item[1]} years old and you are {each_item[2]}")

    student_grade = input("Add student grade: ")
    each_item.append(student_grade)

for each_item in student_list:
    print(f"Student name is {each_item[0]}, you are {each_item[1]} years old and you are {each_item[2]}, and your grades are {each_item[3]}")
1
  • thanks and sorry for all the mistake and not following the direction of website i will be using stack overflow alot from what i can see. thank u again
    – kr3m213
    13 hours ago

Your Answer

kr3m213 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.