# 数字转换星期几

``````s = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
try:
num = int(input("请输入一个1-7之间的整数："))
print(s[num-1])
except:
print("输入有误")
``````

# 水仙花数

``````for i in range(100, 1000):
value = sum([int(s) ** 3 for s in str(i)])
if value == i:
print(i)
``````

# 倒置等腰三角形

print(i)打印*组成的倒置等腰三角形。利用循环语句打印如下图所示的图案。

``````for i in range(4, 0, -1):
for j in range(4-i):
print(" ", end="")
for k in range(2*i-1):
print("* ", end="")
print()
``````

# 随机数判断互质

``````import random

# 定义判断两个数是否互质的函数
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)

# 产生两个100以内的随机整数
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
print(f"{num1}和{num2}", end='')
# 判断两个数是否互质
if gcd(num1, num2) == 1:
print("互质")
else:
print("不互质")

``````

# 输入&输出学生数据

``````def input(num):
L = []
for i in range(num):
n = input("请输入学生姓名：")
if not n:
break
a = input("请输入学生学号：")
b = int(input("请输入语文课程成绩："))
c = int(input("请输入数学课程成绩："))
d = int(input("请输入英语课程成绩："))
L.append({'name': n, 'id': a, 'score': [b, c, d]})
return L

def output(L):
print("+---------------+----------+------+------+------+")
print("|     姓名      |   学号   |  语文|  数学|  英语|")
print("+---------------+----------+------+------+------+")
for d in L:
name = d['name']
id = d['id']
score = d['score']
chinese = score[0]
math = score[1]
english = score[2]
print("|%s|%s|%s|%s|%s|" % (name.center(15), id.center(10), str(chinese).center(6), str(math).center(6), str(english).center(6)))
print("+---------------+----------+------+------+------+")

# 测试input()和output()函数
L = input(5)
output(L)
``````

# 文件读写&字符排序

``````with open('A.txt', 'r') as f1, open('B.txt', 'r') as f2:
s = sorted(s, key=str.lower)
with open('C.txt', 'w') as f3:
f3.write(''.join(s))

``````

# Book类读写&排序

``````class Book():
def __init__(self, name, publisher, price, count):
self.name = name
self.publisher = publisher
self.price = int(price)
self.count = int(count)
f = open('books.txt', 'r')
while v:
v = v.split()
B = Book(v[0], v[1], v[2], v[3])
L.append(B)
return L
def sortBook(L):
L.sort(key=lambda x: (x.publisher, -x.count))
return L
def saveBook(L):
f = open('books.txt', 'w')
for B in L:
f.write(B.name + ' ' + B.publisher + ' ' + str(B.price) + ' ' + str(B.count) + 'n')
print(B.name + ' ' + B.publisher + ' ' + str(B.price) + ' ' + str(B.count))
if __name__=="__main__":
booklist= []
sortBook(booklist)
saveBook(booklist)
``````

# Numpy矩阵操作

``````import numpy as np

# 定义已知矩阵和向量
A = np.array([[1, 2, 3, 4],
[2, 0, 6, 8],
[3, 7, 1, 2],
[8, 1, 1, 2]])
B = np.array([[11, 12, 13, 14],
[12, 10, 16, 18],
[13, 17, 11, 12],
[18, 11, 10, 12]])
y = np.array([[1], [2], [3], [8]])

# 求解未知向量x
B_inv = np.linalg.inv(B)
x = np.dot(np.dot(B_inv, A.T), y)
print(x)
``````

THE END