Python 使用循环创建菱形图案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32613579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a diamond pattern using loops
提问by
I am trying to write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For Example, if the side length is 4, the program should display
我正在尝试编写一个程序,该程序读取一个整数并使用星号显示给定边长的实心菱形。例如,如果边长为 4,则程序应显示
*
***
*****
*******
*****
***
*
Here is what I am trying to do. It is executing, but I can't seem to get the spaces right for the program to show the diamond shape properly....
这就是我想要做的。它正在执行,但我似乎无法为程序找到正确的空间以正确显示菱形....
userInput = int(input("Please input side length of diamond: "))
if userInput > 0:
for i in range(userInput):
for s in range(userInput -3, -2, -1):
print(" ", end="")
for j in range(i * 2 -1):
print("*", end="")
print()
for i in range(userInput, -1, -1):
for j in range(i * 2 -1):
print("*", end="")
print()
Thank you!
谢谢!
回答by Martin Evans
How about the following:
以下情况如何:
side = int(input("Please input side length of diamond: "))
for x in list(range(side)) + list(reversed(range(side-1))):
print('{: <{w1}}{:*<{w2}}'.format('', '', w1=side-x-1, w2=x*2+1))
Giving:
给予:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
So how does it work?
那么它是怎样工作的?
First we need a counter that counts up to side
and then back down again. There is nothing stopping you from appending two range lists together so:
首先,我们需要一个计数器,可以向上计数side
然后再次向下计数。没有什么可以阻止您将两个范围列表附加在一起,因此:
list(range(3)) + list(reversed(range(3-1))
This gives you a list [0, 1, 2, 1, 0]
这给你一个清单 [0, 1, 2, 1, 0]
From here we need to work out the correct number of spaces and asterisks needed for each line:
从这里我们需要计算出每行所需的正确空格数和星号:
* needs 2 spaces 1 asterix
*** needs 1 space 3 asterisks
***** needs 0 spaces 5 asterisks
So two formulas are needed, e.g. for side=3
:
所以需要两个公式,例如side=3
:
x 3-x-1 x*2+1
0 2 1
1 1 3
2 0 5
Using Python's string formatting, it is possible to specify both a fill character and padding width. This avoids having to use string concatenation.
使用 Python 的字符串格式,可以同时指定填充字符和填充宽度。这避免了必须使用字符串连接。
If you are using Python 3.6 or later, you can make use of f
string notation:
如果您使用的是 Python 3.6 或更高版本,则可以使用f
字符串表示法:
for x in list(range(side)) + list(reversed(range(side-1))):
print(f"{'': <{side - x - 1}} {'':*<{x * 2 + 1}}")
回答by Reblochon Masque
This might work better for you:
这可能更适合您:
n = userInput
for idx in range(n-1):
print((n-idx) * ' ' + (2*idx+1) * '*')
for idx in range(n-1, -1, -1):
print((n-idx) * ' ' + (2*idx+1) * '*')
Output for userInput = 6:
userInput 的输出 = 6:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
回答by Reblochon Masque
Thanks Guys, I was able to formulate/correct my code based on the help I received. Thanks for all your input and helping the SO community!
谢谢你们,我能够根据我收到的帮助来制定/更正我的代码。感谢您的所有投入并帮助 SO 社区!
if userInput > 0: # Prevents the computation of negative numbers
for i in range(userInput):
for s in range (userInput - i) : # s is equivalent to to spaces
print(" ", end="")
for j in range((i * 2) - 1):
print("*", end="")
print()
for i in range(userInput, 0, -1):
for s in range (userInput - i) :
print(" ", end="")
for j in range((i * 2) - 1):
print("*", end="")
print()
回答by bison
def build(width):
if width%2==0:
x=[(' *'*i).center(width*2,' ') for i in range(1,(width*2/2))]
else:
x=[(' *'*i).center(width*2+1,' ') for i in range(1,((width*2+1)/2))]
for i in x[:-1]+x[::-1]: print i
This worked for me for any positive width, But there are spaces padding the *s
这对我的任何正宽度都有效,但有空格填充 *s
回答by MaximTitarenko
How about this:
这个怎么样:
n = 5
stars = [('*' + '**'*i).center(2*n + 1) for i in range(n)]
print('\n'.join(stars + stars[::-1][1:]))
The output:
输出:
*
***
*****
*******
*********
*******
*****
***
*
This is similar to some of the answers above, but I find the syntax to be more simple and readable.
这类似于上面的一些答案,但我发现语法更简单易读。