我们的任务是递归开发Pascal三角形,并用红色字体圈出生成的数字。我设法在用户输入后生成了Pascal三角形,但是K如何使数字具有红色字体并被圈出?
在用户输入行数后,我使用递归来实现帕斯卡三角形,但现在我只知道如何将数字圈起来。下面是我使用的代码。
rows = int(input("enter number of rows:"))
list1 = [] #empty list
for i in range(rows):
list2 = [] #sublist to print numbers
for col in range(i+1):
if col == 0 or col == i:
list2.append(1)
else:
list2.append(list1[i - 1][col - 1] + list1[i - 1][col])
list1.append(list2)
for col in range(rows-1-i):
print(format("","<2"), end='')
for col in range(i+1):
print(format(list1[i][col],"<3"), end='')
print()
```
为了使控制台文本变为红色,您可以使用:print("