用Python根据triangle (row)函数打印左下角三角形,右下角三角形,等腰三角形
时间: 2023-04-01 21:01:59 浏览: 137
左下角三角形:
```
def triangle(n):
for i in range(n):
for j in range(i+1):
print("*", end="")
print()
triangle(5)
```
右下角三角形:
```
def triangle(n):
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(i+1):
print("*", end="")
print()
triangle(5)
```
等腰三角形:
```
def triangle(n):
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(2*i+1):
print("*", end="")
print()
triangle(5)
```
相关问题
python无参函数打印等边三角形
以下是使用无参函数打印边长为4的等边三角形的Python代码:
```python
def print_triangle():
n = 4
for line in range(n):
for i in range(n-line-1):
print(" ",end="")
for i in range(line+1):
print("* ",end="")
print()
print_triangle()
```
输出结果为:
```
*
* *
* * *
* * * *
```
Python打印星号等腰三角形
要在Python中打印星号等腰三角形,可以使用循环和条件语句来实现。下面是一个示例代码:
```python
def print_triangle(rows):
for i in range(1, rows+1):
for j in range(rows-i):
print(" ", end="")
for k in range(2*i-1):
print("*", end="")
print()
# 调用函数打印一个5行的等腰三角形
print_triangle(5)
`
这段代码中,`print_triangle`函数接受一个参数`rows`,表示等腰三角形的行数。通过两个嵌套的循环,第一个循环控制行数,第二个循环控制每行的星号数量和空格数量。通过打印空格和星号来构建等腰三角形。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)