(一)python簡單應用

來源:互聯網
上載者:User

標籤:idc   class   判斷   ==   語句   偶數   highlight   密碼   nbsp   

自學之後運用迴圈語句和判斷語句所解決的幾個簡單問題:

1、實現1到10的和:

x = 1he = 0while x < 11:    if x == 7:        pass    else:        he = he + x    x = x + 1print(he)

2、實現1到100的和:

x=1he =0while x< 101:    he=x+he    x=x+1print(he)

3、實現100以內偶數相加:

x = 1while x < 101:    if x % 2 == 0:        print(x)    else:        pass    x = x + 1

4、實現100以內的奇數相加:

x = 1while x < 101:    if x % 2 == 0:        pass    else:        print(x)    x = x + 1

5、實現求1-2+3-4+5 ... 99的所有數的和:

x = 1he = 00while x < 100:    if x % 2 == 0:        he = he - x    else:        he = he + x    x = x + 1print(he)

6、實現三次登陸嘗試:

純IF語句版:

idcard = 123passward = 123x = 0a = input("請輸入你的帳號")c = input("請輸入你的密碼")if a == idcard and c == passward:    print("成功登陸")else:    print("登陸失敗")    a = input("請輸入你的帳號")    c = input("請輸入你的密碼")    if a == idcard and c == passward:        print("成功登陸")    else:        print("登陸失敗")        a = input("請輸入你的帳號")        c = input("請輸入你的密碼")        if a == idcard and c == passward:            print("成功登陸")        else:            print("三次登陸失敗,強制退出")

添加 while迴圈版:

idcared= 123passward = 123x = 0while x < 3:    a = input("請輸入你的帳號")    c = input("請輸入你的密碼")    if a == idcard and c == passward:        print("成功登陸")    else:        print("登陸失敗 請重新輸入")        if x==2 :            print("輸入三次失敗,強制退出")        x = x + 1

  

 

 

經過這幾個問題的簡單嘗試總結了  1、常常會忘記添加迴圈的限制變數。 2、不注意代碼當中的縮排量。

(一)python簡單應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.