標籤:python indentationerror
#!/usr/bin/env python# -*- coding: utf-8 -*-age = 3if age >= 18:print 'adult'elif age >= 6:print 'teenager'else:print 'kid'print '----------------------'names = ['jie', 'bob', 'trcy']for name in names:print nameprint '------------------------'sum = 0for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:sum = sum + xprint sumprint u'-----------range(整數產生)----------------'sum = 0for x in range(101):sum = sum + xprint sumprint u'------while---------------'sum = 0n = 3while n>0:sum = sum + nn = n -2sum = sum - 1print sumprint u'---------輸入raw_input---------------'birth = int(raw_input('birth:')) #注意raw_input輸入的資料都是字元if birth<20:print u'00前'else:print u'00後'
注意代碼儲存的格式是utf-8
:
IndentationError: unindent does not match any outer indentation level
遇到這個錯誤是告訴我們縮排不對
print u‘------while---------------‘
sum = 0
n = 3
while n>0:
sum = sum + n
n = n -2
sum = sum - 1 #容易出現縮排錯誤,Tab和空格的不同,解決辦法是sum = sum - 1移動到上一行,斷行符號讓Editplus幫我們換行
print sum
這裡有趣的事sum = sum - 1位置的不同(前面是否有Tab,決定了sum = sum - 1 語句是否屬於while{}中
python 5 條件判斷和迴圈