我正在使用for循环来获取值并将其存储在3个列表中,并再次使用for循环来获取值,我已经为每个列表制作了一个副本,但是当我尝试打印列表时,副本仍然为空。 / p>
我尝试制作列表的副本,但是没有用。
l_Subjects=[]
l_T_Marks=[]
l_Marks=[]
l_T_Marks1=list(l_Marks)
l_Marks1=l_Marks.copy()
l_Subjects1=l_Subjects.copy()
for i in range(num1):
Subjects=input("\nType subject name:")
l_Subjects.append(Subjects)
while True:
try:
T_Marks= int(input(f'Enter total marks for {Subjects}:'))
l_T_Marks.append(T_Marks)
while True:
Marks = int(input('Enter obtained marks:'))
if Marks>T_Marks:
print('''\n"Please insert correct marks"''')
else:
l_Marks.append(Marks)
break
except ValueError:
print('''\n"Please enter only number"''')
else:
break
print(" _________________________________________________________________________")
print("| |")
print("| Subjects |")
print("|_________________________________________________________________________|")
print("| Component | Marks |")
print("|__________________________________|______________________________________|")
for i in list(l_Marks):
print(f"| {l_Subjects.pop(0)} | {l_Marks.pop(0)}/{l_T_Marks.pop(0)} |")
print(f"|__________________________________|______________________________________|")
print(l_Marks,l_T_Marks,l_Subjects)
print(l_Marks1,l_Subjects1,l_T_Marks1)
Sum_Marks=(sum(l_Marks1))
Sum_T_Marks=(sum(l_T_Marks1))
答案 0 :(得分:0)
您用于复制列表的代码,在for循环之后使用该代码(当for循环在列表中插入值然后对其进行复制时),在for循环之前复制列表将为空,因为那时列表是empty.i更正了代码(如下所示)
l_Subjects=[]
l_T_Marks=[]
l_Marks=[]
for i in range(num1):
Subjects=input("\nType subject name:")
l_Subjects.append(Subjects)
while True:
try:
T_Marks= int(input(f'Enter total marks for {Subjects}:'))
l_T_Marks.append(T_Marks)
while True:
Marks = int(input('Enter obtained marks:'))
if Marks>T_Marks:
print('''\n"Please insert correct marks"''')
else:
l_Marks.append(Marks)
break
except ValueError:
print('''\n"Please enter only number"''')
else:
break
l_T_Marks1=list(l_Marks)
l_Marks1=l_Marks.copy()
l_Subjects1=l_Subjects.copy()
print(" _________________________________________________________________________")
print("| |")
print("| Subjects |")
print("|_________________________________________________________________________|")
print("| Component | Marks |")
print("|__________________________________|______________________________________|")
for i in list(l_Marks):
print(f"| {l_Subjects.pop(0)} | {l_Marks.pop(0)}/{l_T_Marks.pop(0)} |")
print(f"|__________________________________|______________________________________|")
print(l_Marks,l_T_Marks,l_Subjects)
print(l_Marks1,l_Subjects1,l_T_Marks1)
Sum_Marks=(sum(l_Marks1))
Sum_T_Marks=(sum(l_T_Marks1))