1.
#A.
num = int(input("请输入要打印的层数:"))
for n in range(1, num+1):
s = ""
for i in range(1, n+1):
s += f"{i}" + " "
print(s)
#B.
num = int(input("请输入要打印的层数:"))
for i in range(num+1, 0, -1):
s=" "
for j in range(1, i, 1):
s+=f"{j}" + " "
print(s)
#C.
num = int(input("请输入要打印的层数:"))
for i in range(1, num + 1):
s = ""
for k in range(num - i):
s += " " * 2
for j in range(0, i):
s += f"{(i - j)}" + " "
print(s)
#D.
num = int(input("请输入要打印的层数:"))
for i in range(num + 1, 0, -1):
s = ""
for k in range(num + 1 - i):
s += " " * 2
for j in range(i - 1, 0, -1):
s += f"{(i - j)}" + " "
print(s)
2.
n = input("Enter an integer:")
n = n[3::-1]
print(f"The reversed number is {n}")
运行结果如下:
3.
n = int(input("Enter an integer:"))
m = n % 5 == 0 and n % 6 == 0
print(f"Is {n} divisible by 5 and 6?", m)
m = n % 5 == 0 or n % 6 == 0
print(f"Is {n} divisible by 5 or 6?", m)
m = not (n % 5 == 0 and n % 6 == 0)
print(f"Is {n} divisible by 5 and 6,but not both", m)