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)
CodePudding user response:
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]}")
CodePudding user response:
wow thanks for fast and so effective answer it works now. I was trying to do this for 2h. Thanks a lot Barmar