classes = input ("what class are you in ? class1, class2 or class 3? ")
if classes == ("class1"):
students = input (str("how many students are in your class "))
while int(students)>int(student):
name = input ("what is your name ")
score = input ("what is your score ")
student = student + 1
name_arr = []
name_arr.append(name)
name_arr.append(score)
print(name_arr)
elif classes == ("class2"):
students = input (str("how many students are in your class "))
while int(students)>int(student):
student = student + 1
name1 = input ("what is your name ")
score1 = input ("what is your score ")
name_arr = []
name_arr.append(name)
name_arr.append(score)
print(name_arr)
elif classes == ("class3"):
students = input (str("how many students are in your class "))
while int(students)>int(student):
student = student + 1
name2 = input ("what is your name ")
score2 = input ("what is your score ")
name_arr = []
name_arr.append(name)
name_arr.append(score)
print(name_arr)
当我运行代码时,名称和分数逐个打印而不是一个大数组![在此输入图像描述] [1]
我需要将所有名称和所有分数放在一个大数组而不是一个数组上
答案 0 :(得分:0)
您需要在循环之外初始化数组:
name_arr = []
while int(students)>int(student):
name = input ("what is your name ")
score = input ("what is your score ")
student = student + 1
name_arr.append(name)
name_arr.append(score)
print(name_arr)
否则你只需将它设置回每个循环的空列表。
答案 1 :(得分:0)
我认为这会对你有帮助
classes = input ("what class are you in ? class1, class2 or class 3? ")
MAIN = []
if classes == "class1":
students = input (str("how many students are in your class "))
student=0
while int(students)>int(student):
name = input ("what is your name ")
score = input ("what is your score ")
student = student + 1
MAIN.append(name)
MAIN.append(score)
elif classes == "class2":
students = input (str("how many students are in your class "))
student=0
while int(students)>int(student):
student = student + 1
name1 = input ("what is your name ")
score1 = input ("what is your score ")
MAIN.append(name1)
MAIN.append(score1)
elif classes == ("class3"):
student=0
students = input (str("how many students are in your class "))
while int(students)>int(student):
student = student + 1
name2 = input ("what is your name ")
score2 = input ("what is your score ")
MAIN.append(name2)
MAIN.append(score2)