Python缩进问题
生活随笔
收集整理的這篇文章主要介紹了
Python缩进问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python縮進問題
Python中,是通過代碼的縮進,來決定代碼的邏輯的。通俗的說,Python中的代碼的縮進,不是為了好看,而是覺得代碼的含義,上下行代碼之間的關系。縮進弄錯了,就會導致程序出錯,執行結果變成不是你想要的了。
強制縮進其實是Python保持代碼風格統一且可讀性良好的機制。
python是用縮進來標識語句塊的。
學python需要游標卡尺。
import csvcand_path = './candidates.csv' def readCSV(filename): lines = [] with open(filename, "rb") as f: csvreader = csv.reader(f) for line in csvreader: lines.append(line) return lines cands = readCSV(cand_path) print candsreturn的位置縮進不一樣,會導致不一樣的結果
import csvcand_path = './candidates.csv' def readCSV(filename): lines = [] with open(filename, "rb") as f: csvreader = csv.reader(f) for line in csvreader: lines.append(line) return lines cands = readCSV(cand_path) print cands測試結果
import csv cand_path = './candidates.csv' lines = [] with open('./candidates.csv', "rb") as f: csvreader = csv.reader(f) for line in csvreader: lines.append(line) print lines解決方法
采用sublime 或者Pycharm編輯來減少Python縮進問題。
python問題:IndentationError:expected an indented block錯誤
Python語言是一款對縮進非常敏感的語言,最常見的情況是tab和空格的混用會導致錯誤,或者縮進不對。
s = 200 if s >=0: print sFile "C:\Users\bids\Desktop\test.py", line 3 print s^ IndentationError:expected an indented block在編譯時會出現這樣的錯IndentationError:expected an indented block說明此處需要縮進,你只要在出現錯誤的那一行,按空格或Tab(但不能混用)鍵縮進就行。
s = 200 if s >=0:print s elseprint -sReferences
http://blog.csdn.net/qq_15437667/article/details/52558999
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python缩进问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下设置环境变量
- 下一篇: 【并行计算-CUDA开发】CUDA线程、