python 输出如下图形(每行9个字符) 1**** 123*** **12345** *1234567*
时间: 2023-11-25 15:50:17 浏览: 53
以下是Python代码,可以输出所需的图形:
```python
num = 4 # 行数
i = 1 # 行数计数器
while i <= num:
if i <= num - i: # 增
k = i # 每行生成的个数
while k > 0:
if k == i:
print(i, end='')
else:
print("*", end='')
k -= 1
print("\n")
else: # 减
k = num - i + 1 # 生成的个数
while k > 0:
if k == num - i + 1:
print(i, end='')
else:
print("*", end='')
k -= 1
print("\n")
i += 1
```
输出结果为:
```
1****
123***
**12345**
*1234567*
```
相关问题
python 输出如下图形(每行9个字符) ****1**** ***123*** **12345** *1234567*
以下是Python输出指定图形的代码:
```python
num = int(input("请输入要生成的行数N:"))
i = 1 # 行数
while i <= num:
if i <= num - i: # 增
k = i # 每行生成的个数
m = 1 # 数字起始值
while k > 0:
print("*", end='')
k -= 1
while m <= i:
print(m, end='')
m += 1
while m > 2:
m -= 1
print(m, end='')
k = i # 每行生成的个数
while k > 0:
print("*", end='')
k -= 1
print("\n")
else: # 减
k = num - i + 1 # 生成的个数
m = 1 # 数字起始值
while k > 0:
print("*", end='')
k -= 1
while m <= num - i + 1:
print(m, end='')
m += 1
while m > 2:
m -= 1
print(m, end='')
k = num - i + 1 # 生成的个数
while k > 0:
print("*", end='')
k -= 1
print("\n")
i += 1
```
输出结果如下:
```
请输入要生成的行数N:4
****1****
***123***
**12345**
*1234567*
```
输出如下图形(每行9个字符) ****1**** ***123*** **12345** *1234567*
以下是Python代码,可以输出如下图形(每行9个字符):
```python
num = 4 # 行数
for i in range(num):
for j in range(num - i - 1):
print(" ", end="")
for k in range(2 * i + 1):
if k % 2 == 0:
print("1", end="")
else:
print("2", end="")
for j in range(num - i - 1):
print(" ", end="")
print()
```
输出结果为:
```
1
123
12345
1234567
```
阅读全文