71编写input()和output()函数输入,输出5个学生的数据记录
N = 3
student = []
for i in range(5):
student.append(['','',[]])
print(student)
def input_stu(stu):
for i in range(N):
stu[i][0] = input("input student num:")
stu[i][1] = input("input student name:")
for j in range(3):
stu[i][2].append(int(input('score:')))
def output_stu(stu):
for i in range(N):
print('%-6s%-10s' % (stu[i][0],stu[i][1]))
for j in range(3):
print('%-8d' % stu[i][2][j])
input_stu(student)
print(student)
output_stu(student) #输出格式的设置
[['', '', []], ['', '', []], ['', '', []], ['', '', []], ['', '', []]]
input student num:1
input student name:aa
score:12
score:12
score:12
input student num:2
input student name:bb
score:21
score:21
score:21
input student num:3
input student name:cc
score:14
score:14
score:14
[['1', 'aa', [12, 12, 12]], ['2', 'bb', [21, 21, 21]], ['3', 'cc', [14, 14, 14]], ['', '', []], ['', '', []]]
1 aa
12
12
12
2 bb
21
21
21
3 cc
14
14
14
a = [1,3,1,2,3,4,5,6]
print('%-6s%-10s' % (a[0], a[1]))#两个输出之间空6格
print('%s%s' % (a[0], a[1])) #两个输出之间没有空格
print('%s%+2s' % (a[0], a[1])) #两个输出之间空1格
print('%-8s%-8s' % (a[0], a[1])) #两个输出之间空7格
print('%1s%s' % (a[0], a[1])) #%1s和%1s均代表第一个输出不空格
print('%d' % a[2])
1 3
13
1 3
1 3
13
1
#根据输入生成一个矩阵
a= []
for i in range(5):
num = int(input('please input a number:\n'))
a.append(num)
print(a)
#生成自然数的矩阵
n = int(input("input a numuber:\n"))
a= [i for i in range(1,n+1)]
print(a)
#反向输出一个列表
n = int(input("input a numuber:\n"))
a= [i for i in range(1,n+1)]
print(a)
a.reverse() #将列表反转
print(a)
#根据输入生成一个矩阵
a= []
for i in range(5):
num = int(input('please input a number:\n'))
a.append(num)
print('输入的矩阵a为',a)
#列表排序及连接:
#排序可使用 sort() 方法,连接可以使用 + 号或 extend() 方法
a.sort()
b = [11,12,13]
c = [21,22,23]
a.extend(b)
print("a+b矩阵为",a)
print('a+c矩阵为',a+c)