劉任昌Python自訂函數迴圈write模式a與w

上面的程式碼

<style>
  h1{background-color: purple; color: white; border: 10px solid red; text-align:center; padding: 5px}
  ul{background-color: green; color: white; font-size: 2em;line-height:1.5}
</style>

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('我是大正妹\n')
print('我討厭臭男生',end='')
print('我喜歡女生')
print('我喜歡胖胖的女生',file=f)
for i in range(n):
    row1(k,m)
    row2(k,m)
f.close()

期中考筆試重點

  • write寫入檔案模式mode w=write會覆蓋原有檔案, a=append接續原來檔案, x=create創立新檔案, x與w有何不同?
  • write寫入檔案,預設不換行,換行'\n'
  • print呈現在螢幕,預設換行,不換行end=''。
  • write寫入用到英文以外的字元(一,a,b)encoding='utf8'

教學影片386

留言

  1. https://wilson11122.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  2. https://aks8704100.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  3. https://addison030.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  4. https://yun0205.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  5. https://ctz642895.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  6. https://keiime.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  7. https://hui026.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  8. https://whitexd.blogspot.com/2024/03/python.html

    回覆刪除
  9. https://rrrrr-eeeee.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  10. https://kk0725.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  11. https://oscarliu1688.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  12. https://tustning.blogspot.com/2024/03/pythonwriterange.html

    回覆刪除
  13. https://xi715.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  14. https://grace8166.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  15. https://chin-0908.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除
  16. https://pcai766.blogspot.com/2024/03/pythonwriteaw.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

柯聞折Javascript物件導向方法是物件的函數

甲劉任昌python串列List(陣列Array),元組Tuple,集合Set,字典Dict

劉任昌PYTHON陣列ARRAY串列LIST字典DIC集合SET元組TUPLE