def diamond(size, sym_func):
s = ''
for row in xrange(size):
for column in xrange(size):
if row > size//2: # if bottom half reflect top
row = size - row - 1
if column > size//2: # if right half reflect left
column = size - column - 1
s += sym_func(row,column,size)
s+= '\n'
return s
def solid(row,column,size):
if column >= (size // 2 - row):
return "*"
return " "
def hollow(row,column,size):
if column == (size // 2 - row):
return "*"
return " "
def circle(row,column,size):
if (size//2-row)**2+(size//2-column)**2 <= (size//2)**2:
return '*'
return ' '
print diamond(size=7, sym_func=solid) # The size of the diamond
print diamond(size=7, sym_func=hollow) # The size of the diamond
print diamond(size=17, sym_func=circle) # The size of the diamond
看看空心和實心符號功能之間的區別,如果您使用的是> =那麼你得到了堅實的事情,如果你使用==進行精確comperison那麼它僅僅是parimiter
哎呀,你打我:) – nephi12