0
我有一個任務,我必須寫一個函數,它將打印一個矩形(用戶可以改變它的高度和寬度)框架內的「x」 (我盡我所能來形容它,但我希望圖像會有所幫助)。簡單的矩形終端動畫在Python 3.x
在我的代碼中,我將frame表示爲行列表(用「o」s填充)。我已經有了一個函數來查找矩形的中心,並用「x」替換中間的「o」 - 這是我的第一幀。
本來我想讓所有剩餘的框架都嵌套循環,但由於有兩條水平線「x」es,所以它相當快地混淆了。
我很確定我需要一個新的「座標系」來遍歷幀。有人能請我指出正確的方向嗎?
def squareAnimation():
while True:
frameHeight = int(input("Please enter frame's height: \nRemember: Odd numbers only!\n"))
frameWidth = int(input("Please enter frame's width:\nRemember: Odd numbers only!\n"))
if (frameHeight % 2 != 0 and frameWidth % 2 != 0):
break
numberOfFrames = min(int(frameWidth/2), int(frameHeight/2))
lineTemplate = "o " * frameWidth
#frame filled with "o" only
blankFrame = [lineTemplate] * frameHeight
#frame with x in center
frameTemplate = blankFrame
findListCenter(frameTemplate, frameHeight, frameWidth)
printListWithoutBrackets(frameTemplate)
def findListCenter(frame, height, width):
frame[int(height/2)] = "o " * (int(width/2)) + "x " + "o " * (int(width/2))
def printListWithoutBrackets(frame):
for object in range(0, len(frame)):
tempLine = frame[object]
print("".join(tempLine))
squareAnimation()