Python循环反向菱形

2024-06-09 19:02:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个循环创建菱形的代码,但我希望它被反转。在

width = int(input("Please enter a width: "))

i = 1
while i < width*2:
    if i < width:
        print("-" * (width-i) + " *" * i + "-" * (width-i))
    else:
        print("-" * (i-width) + " *" * (2*width-i) + "-" * (i-width))
    i += 1 [EDIT: formatting mistake]

我的输出如下:

^{pr2}$

但我希望它是这样的:

 * * * * *
- * * * *-
-- * * *--
--- * *---
---- *----
---- *----
--- * *---
-- * * *--
- * * * *-
 * * * * *

请帮忙!在


Tags: 代码inputifwidtheditelseintprint
1条回答
网友
1楼 · 发布于 2024-06-09 19:02:03
width = int(input("Please enter a width: "))

i = 0
while i < width*2:
    if i < width:
        print("-" * i+ " *" * (width-i) + "-" * i)
    else:
        print("-" * ((2*width-i) -1) + " *" * (i - width + 1) + "-" *     ((2*width-i) -1))
    i += 1 

相关问题 更多 >