解決python對齊錯誤的方法
運(yùn)行的時候,有時候會出現(xiàn)語法錯誤: IndentationError: unexpected indent
可以用如下方法解決:
首先把空格顯示出來,空格的地方 ,由點代替
修改把tab 代表4個位置
然后格式就對齊了。
實例擴(kuò)展:
如何解決文本對齊
大家好,我是python學(xué)習(xí)新手,我在一個練習(xí)題目中遇到問題.
題目的要求是把列表打印輸出并對齊。
輸入數(shù)據(jù):
tableData = [[’apples’, ’oranges’, ’cherries’, ’banana’],[’Alice’, ’Bob’, ’Carol’, ’David’],[’dogs’, ’cats’, ’moose’, ’goose’]]
要求的輸出數(shù)據(jù)(第一行右對齊,其他左對齊):
apples Alice dogs oranges Bob catscherries Carol moose banana David goose
以下是我的代碼
'''下面是代碼正文'''tableData = [[’apples’, ’oranges’, ’cherries’, ’banana’], [’Alice’, ’Bob’, ’Carol’, ’David’], [’dogs’, ’cats’, ’moose’, ’goose’]]def printTable(tableData): # 下面是為了求每個內(nèi)層列表的最長字符串的長度 colWidths = [0] * len(tableData) for i in range(len(colWidths)): colWidths[i] = len(sorted(tableData[i], key=(lambda x: len(x)))[-1]) for x in range(len(tableData[0])): for y in range(len(tableData)): print(tableData[y][x].rjust(colWidths[y]), end=’ ’) print(’’) # 換行printTable(tableData)
輸出結(jié)果是(全部右對齊了):
apples Alice dogs oranges Bob cats cherries Carol moose banana David goose
到此這篇關(guān)于解決python對齊錯誤的方法的文章就介紹到這了,更多相關(guān)python對齊錯誤如何解決內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. layui的checbox在Ajax局部刷新下的設(shè)置方法2. Intellij IDEA 關(guān)閉和開啟自動更新的提示?3. Java Swing權(quán)威指南:Spinner Model Controls4. IntelliJ IDEA設(shè)置編碼格式的方法5. IntelliJ IDEA導(dǎo)出項目的方法6. ASP.NET Identity的基本用法7. web下載文件和跳轉(zhuǎn)的方法8. ASP.NET Core WebSocket集群實現(xiàn)思路詳解9. uni-app結(jié)合.NET 7實現(xiàn)微信小程序訂閱消息推送10. JavaWeb Servlet中url-pattern的使用
