

帮帮忙吧!真是没思路了,都试了好多遍了,没结果。学不会了,提供一个思路吧
对您有帮助,请点击我的回答左下角
“采纳该答案”
一、诗歌排版用zip()打包诗句
#!/usr/bin/nve python
# coding: utf-8
# data 准备
title = '︿︿小︿池︿︿' # 凑够诗句长度。
author = '*宋/杨万里*' # 凑够诗句长度。
first = '泉眼无声惜细流'
second = '树荫照水爱晴柔'
third = '小荷才露尖尖角'
fourth = '早有蜻蜓立上头'
# 拼接处理
poem = map(lambda x: ' '.join(x), zip(fourth, third, second, first, author, title))
# 轮询输出
print(f"\n\n{'':~^50}\n\n")
for line in poem:
print(f"{line:^43}")
print(f"\n\n{'':~^50}\n\n")

二、矩阵对角和用二维数组下标
#!/usr/bin/nve python
# coding: utf-8
# 输入
scores = [] # 成绩列表。
for i in 'ab':
a, b = map(int, input(f"\n{'输入分数(如1 5):':>15}").split())
scores.append((a, b))
# 轮询输出
print(f"\n\n{'':~^50}\n\n{' 成绩矩阵 ':*^46}")
for i in scores:
print(f"\n{i[0]:>23}{i[1]:>4}")
print(f"\n{'*'*50}\n\n{'主对角线和:':>17}{scores[0][0] + scores[1][1]},次对角线和:{scores[0][1] + scores[1][0]}\n\n{'':~^50}\n\n")
由于有倍受高版本Python推崇的“插值字符串格式化”(个人觉得,用起来就就像自然语言一样“得心应手”,一段输出都可以一条语句搞定🤗)好用,代码中就不用新式字符串格式化了。
'成绩一:{},成绩二:{}'.format(98, 67)


对您有帮助,请点击左边
“采纳该答案”
m = eval(input())
prime = 0
secondary = 0
for i in range(len(m)):
prime += m[i][i]
secondary += m[i][len(m)-i-1]
print("主对角线之和={}".format(prime))
print("次对角线之和={}".format(secondary))
class Solution(object):
def __init__(self):
pass
def sum_main_diagonal(self, mytuple):
mysum = 0
width = len(mytuple[0])
height = len(mytuple)
if width != height:
mysum = mytuple[0][0] + mytuple[-1][-1]
else:
for i in range(width):
mysum += mytuple[i][i]
return mysum
def sum_minor_diagonal(self, mytuple):
mysum2 = 0
width = len(mytuple[0])
height = len(mytuple)
if width != height:
mysum2 += mytuple[0][-1] + mytuple[-1][0]
else:
for i in range(width):
mysum2 += mytuple[i][width - 1 - i]
return mysum2
if __name__ == "__main__":
s = Solution()
mytuple = eval(input())
print("主对角线之和={}".format(s.sum_main_diagonal(mytuple)))
print("次对角线之和={}".format(s.sum_minor_diagonal(mytuple)))