python边长为3菱形while循环
时间: 2024-05-27 12:13:00 浏览: 10
实现的代码是什么?
以下是实现代码:
n = 3
i = 1
while i <= n:
print(' ' * (n - i) + '* ' * i)
i += 1
while i > 1:
i -= 1
print(' ' * (n - i) + '* ' * i)
相关问题
python while循环打印菱形
以下是使用Python编写的打印菱形的代码:
```
n = int(input("请输入菱形的行数:"))
i = 1
while i <= n:
j = 1
while j <= n - i:
print(" ", end="")
j += 1
j = 1
while j <= 2 * i - 1:
print("*", end="")
j += 1
print()
i += 1
i = n - 1
while i >= 1:
j = 1
while j <= n - i:
print(" ", end="")
j += 1
j = 1
while j <= 2 * i - 1:
print("*", end="")
j += 1
print()
i -= 1
```
这个程序会要求用户输入菱形的行数,然后使用两个while循环来打印出菱形。第一个while循环用来打印上半部分的菱形,第二个while循环用来打印下半部分的菱形。在每个while循环中,我们使用另外两个while循环来打印出空格和星号。
python用while循环打菱形
Sure, I can help you with that! Here's an example code to print a diamond using while loop in Python:
```
n = int(input("Enter the number of rows: ")) # take input from user
# upper part of diamond
i = 1
while i <= n:
print(" " * (n - i), end="")
print("* " * i)
i += 1
# lower part of diamond
i = n-1
while i > 0:
print(" " * (n - i), end="")
print("* " * i)
i -= 1
```
Hope this helps! Let me know if you have any other questions.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)