到目前为止,这是我的代码:
n = int(input("Please enter a number between 1 and 10: "))
if n > 10:
n = int(input("Please enter a smaller number (must not be greater than 10): "))
for k in range(1, n+1):
print(k, end=" ")
print('-' * 10, end=" ")
print()
for i in range(1, n+1):
print(i, end=" ")
for j in range(1, n + 1):
print(i * j, end=" ")
print()
假设用户输入4为n,我需要显示一个如下所示的乘法表:
1 2 3 4
----------
1 | 1 2 3 4
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16
当前标题如下:
1 ----------- 2 ---------- 3 ----------- 4 ----------
我想念什么?破折号应位于自己的行上。我曾尝试在包括i和j循环在内的不同位置插入标头的行,包括破折号。
答案 0 :(得分:0)
您需要将行print('-' * 10, end=" ")
移到for循环之外,因为您不希望脚本打印该标题行一次而不是n次。除此之外,您只需要在行索引之后添加一个小节即可获得向下的边界线。
n = int(input("Please enter a number between 1 and 10: "))
if n > 10:
n = int(input("Please enter a smaller number (must not be greater than 10): "))
for k in range(1, n+1):
print(k, end=" ")
print()
print('-' * 10)
for i in range(1, n+1):
print(i, "|", end=" ")
for j in range(1, n + 1):
print(i * j, end=" ")
print()
答案 1 :(得分:0)
您可以使用
n = int(input("Please enter a number between 1 and 10: "))
if n > 10:
n = int(input("Please enter a smaller number (must not be greater than 10): "))
for k in range(1, n+1):
print(k, end=" ")
# print('-' * 10, end=" ")
print()
print("----------")
for i in range(1, n+1):
print(i, end=" | ")
for j in range(1, n + 1):
print(i * j, end=" ")
print()
输出
Please enter a number between 1 and 10: 4
1 2 3 4
----------
1 | 1 2 3 4
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16