"给定列表list1和list2具有相同的长度,创建一个新列表,其中包含list1的第一个元素,后跟list2的第一个元素,后跟list1的第二个元素,后跟list2的第二个元素,以及等等(换句话说,新列表应该由list1和list2的交替元素组成).例如,如果list1包含[1,2,3]并且list2包含[4,5,6],那么新列表应该包含[1,4,2,5,3,6].将新列表与变量list3相关联."
list1 = []
list2 = []
list3 = []
for i in range(len(list3)):
list3.append(list1)
list3.append(list2)
Run Code Online (Sandbox Code Playgroud)
我很确定这是错误的.我应该改进什么?顺便说一句,我认为这必须包括len和范围.
该程序应该读取.txt文件的各行,并找到每个人的测试分数的平均值.记事本中的我的.txt文件看起来像这样......
John Smith 5 9 8 10 7
Mary Brown 3 10 8 4 2
Bob Black 7 10 9 10 8
Run Code Online (Sandbox Code Playgroud)
导入数据文件后的最终程序应如下所示......
Filename? file1.txt ## file1.txt = the name of the data file the user input
John Smith 7.8
Mary Brown 5.4
Bob Black 8.8
Average over all students = 7.333333333333333
Run Code Online (Sandbox Code Playgroud)
这是我的Python代码......
def main():
filename = input("Filename? ")
filename = filename + ".txt"
with open(filename,"r") as file:
for lines in file:
wholefile = lines.strip()
print(wholefile)
print("")
average = 7.333333333333333 …Run Code Online (Sandbox Code Playgroud)