Python的幾個小程式,其實我覺得可以稱作初學時的基礎演算法

來源:互聯網
上載者:User

標籤:基礎   pass   整理   數值   blog   密碼   時光   span   int   

      昨天學習的,今天做一下整理,以前學過幾天c,感覺什麼都沒有搞出來,有點泄氣,看到Python後試試,從最基本的東西學起,希望不要辜負我的這一點熱情。

if語句的應用

 1 n=1 2 while n<5: 3     inp = input(‘請輸入會員層級:‘) 4     if inp == "進階會員": 5         print(‘貴賓1‘) 6     elif inp =="白金會員": 7         print("貴賓2") 8     elif inp ==‘鉑金會員‘: 9         print("貴賓3")10     else:11         print("城管")12 13     print(‘該幹嘛幹嘛去‘)14     n+=115 pass16 print(‘窮逼,滾!‘)

列出1-100的偶數:

n=1while n<101:    if n==1:        pass    elif n%2==1:            #利用奇數的性質,不能被2整除,餘數是1,因此,%(求模)判斷餘數是不是1        pass    else:        print(n)    n+=1

列出數字1 2 3 4 5 6 8 9 10:

count=1while count<11:    if count==7:        #判斷迴圈後的數值是不是7        pass                 #c語言中沒有這個關鍵字(不知道這個稱呼對不,有的好像叫保留字)    else:        print(count)    count+=1print(‘-----END------‘)

經典的高斯演算法:1-100的累加,懂的5050

n=1sum=0while n<101:    sum=sum+n    n=n+1print(sum)

100以內的奇數

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

計算1-2+3-4+5.....,我最後算的是-50,不知道對不

n=1sum=0while n<101:    t=n%2    if t==0:        sum=sum-n    else:        sum=sum+n    n=n+1print(sum)

輸入三次密碼,不正確就退出,感覺有點怪怪的,不清楚

n=1while n<4:        mima=input(‘請輸入密碼:‘)                      #有點疑惑,與c不一樣的是要求輸入密碼時游標跳到前面去了        if mima ==‘123‘:            print(‘小草包,進來吧!‘)            break        else:            print(‘請重新輸入‘)        n+=1        print(‘你輸入的次數超過3次,儘快滾蛋!否則警示‘)

 

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.