2016-06-09 30 views
3

我需要一些幫助,試圖解決這個打印三角形,現在它保持垂直僅打印如何使用循環

I need it to come out as

height = int(input("Height of triangle: ")) 
for x in range(height): 
    for y in range(height): 
    print("#",end = '') 
    print() 
+1

'對於範圍(x)中的y''會給你動態的行長度! – schwobaseggl

+1

那些不是很好的三角形。你確定你不想在頂部和右下角的頂點? –

+0

我嘗試了範圍(x)中的y,但它的工作原理是什麼,但是您知道如何獲得三角形中間的空間嗎? – Miryloth

回答

2

這裏是我的解決方案,它包括使用蓄電池:

height = int(input("Height of triangle: ")) 
count = 0 

for i in range(height-1): 
    print('#' + ' '*count + '#') 
    count += 1 

print('#'*height) 
0
height = 6 

for rowIndex in xrange(height-1): 
    row = [' ']*height   # yields array of size height 
    row[0] = row[rowIndex+1] = '#' 
    print (''.join(row)) # This makes string from array 
print ('#'*height)   # Print lower side of triangle 

您還可以刪除 「+1」 第5行以獲得更多的 「前衛」 TRIANG LES。