数字金字塔
Posted wang102030
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数字金字塔相关的知识,希望对你有一定的参考价值。
num = eval(input("请输入一个整数:")) for i in range(1, num + 1): for j in range(num-i, 0, -1): print(" ", end=" ") for j in range(i, 1, -1): print(j, end=" ") for j in range(1, i+1): print(j, end=" ") print(" ")
row = int(input("请输入行数:")) num = 1 sum = 0 for i in range(1, row + 1): for j in range(row - i, 0, -1): print(" ", end=" ") for j in range(1, i + 1): print(num, end=" ") num *= 2 sum = num num = 1 sum //= 2 for j in range(2, i + 1): print(sum//2, end=" ") sum //= 2 print("")
pattern = input("请输入你要选择的模式:") while True: if pattern == "A": row = eval(input("请输入行数:")) for i in range(1, row + 1): for j in range(1, i+1): print(j, end=" ") print("") elif pattern == "B": row = eval(input("请输入行数:")) for i in range(1, row + 1): for j in range(1, row - i + 2): print(j, end=" ") print("") elif pattern == "C": row = eval(input("请输入行数:")) for i in range(1, row + 1): for j in range(row - i, 0, -1): print(" ", end=" ") for j in range(i, 0, -1): print(j, end=" ") print("") elif pattern == "D": row = eval(input("请输入行数:")) for i in range(1, row + 1): for j in range(1, i): print(" ", end=" ") for j in range(1, row - i + 2 ): print(j, end=" ") print("") elif pattern == "退出": break else: pattern = input("输入错误,请从新输入")
以上是关于数字金字塔的主要内容,如果未能解决你的问题,请参考以下文章