python 5 條件判斷和迴圈

來源:互聯網
上載者:User

標籤: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 條件判斷和迴圈

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.