范雅惠Python自訂函數迴圈write模式a與w
VS code截圖
程式碼
space, slash, backslash, cr = ' ', '/', '\\', '\n'def row1(k,m):#定義自訂函數
for i in range(1, k+1):
for ii in range(m):
for j in range(k-i):
f.write(space)
f.write(slash)
for j in range(2*i-2):
f.write(space)
f.write(backslash)
for j in range(k-i):
f.write(space)
f.write(cr)
def row2(r,m):
for i in range(1, k+1):
for ii in range(m):
for j in range(i-1):
f.write(space)
f.write(backslash)
for j in range(2*k-2*i):
f.write(space)
f.write(slash)
for j in range(i-1):
f.write(space)
f.write('\n')
k = input('輸入三角形長度: ')
m = input('輸入橫向規格: ')
n = input('往下重複次數: ')
k, m, n = int(k), int(m), int(n) #將字串k轉integer整數append
f = open("范雅惠.txt",'w',encoding='utf8')
f.write('寫入w蓋掉原來檔案')
f.write('寫入a=append接續來檔案')
for i in range(n):
row1(k,m)
row2(k,m)
f.close()
评论
发表评论