python全棧開發【第四篇】Python流程式控制制

來源:互聯網
上載者:User

標籤:程式   一起   列印   又能   count   print   choice   程式員   超級   

十二 流程式控制制之if…else
既然我們編程的目的是為了控制電腦能夠像人腦一樣工作,那麼人腦能做什麼,就需要程式中有相應的機制去類比。人腦無非是數學運算和邏輯運算,對於數學運算在上一節我們已經說過了。對於邏輯運算,即人根據外部條件的變化而做出不同的反映,比如1 如果:女人的年齡>30歲,那麼:叫阿姨
age_of_girl=31 if age_of_girl > 30: print(‘阿姨好‘) 

2 如果:女人的年齡>30歲,那麼:叫阿姨,否則:叫小姐 

age_of_girl=18 if age_of_girl > 30: print(‘阿姨好‘) else: print(‘小姐好‘) 

  


3 如果:女人的年齡>=18並且<22歲並且身高>170並且體重<100並且是漂亮的,那麼:表白,否則:叫阿姨

age_of_girl=18 height=171 weight=99 is_pretty=True if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: print(‘表白…‘)else: print(‘阿姨好‘)在表白的基礎上繼續: 如果表白成功,那麼:在一起 否則:列印。。。age_of_girl=18 height=171 weight=99 is_pretty=Truesuccess=Falseif age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: if success: print(‘表白成功,在一起‘) else: print(‘什麼愛情不愛情的,愛nmlgb的愛情,愛nmlg啊…‘) else: print(‘阿姨好‘)

  

4 如果:成績>=90,那麼:優秀   如果成績>=80且<90,那麼:良好   如果成績>=70且<80,那麼:普通   其他情況:很差
score=input(‘>>: ‘) score=int(score)if score >= 90: print(‘優秀‘) elif score >= 80: print(‘良好‘) elif score >= 70: print(‘普通‘) else: print(‘很差‘)  if 條件1:    縮排的代碼塊  elif 條件2:    縮排的代碼塊  elif 條件3:    縮排的代碼塊  ……  else:      縮排的代碼塊

  

!/usr/bin/env pythonname=input(‘請輸入使用者名稱字:‘) password=input(‘請輸入密碼:‘)if name == ‘egon‘ and password == ‘123‘:   print(‘egon login success‘) else:   print(‘使用者名稱或密碼錯誤‘)
練習一:使用者登陸驗證

  

#!/usr/bin/env python#根據使用者輸入內容列印其許可權‘‘‘egon --> 超級管理員tom  --> 普通管理員jack,rain --> 業務主管其他 --> 普通使用者‘‘‘name=input(‘請輸入使用者名稱字:‘)if name == ‘egon‘:    print(‘超級管理員‘)elif name == ‘tom‘:    print(‘普通管理員‘)elif name == ‘jack‘ or name == ‘rain‘:    print(‘業務主管‘)else:    print(‘普通使用者‘)練習二:根據使用者輸入內容輸出其許可權

 

# 如果:今天是Monday,那麼:上班# 如果:今天是Tuesday,那麼:上班# 如果:今天是Wednesday,那麼:上班# 如果:今天是Thursday,那麼:上班# 如果:今天是Friday,那麼:上班# 如果:今天是Saturday,那麼:出去浪# 如果:今天是Sunday,那麼:出去浪#方式一:today=input(‘>>: ‘)if today == ‘Monday‘:    print(‘上班‘)elif today == ‘Tuesday‘:    print(‘上班‘)elif today == ‘Wednesday‘:    print(‘上班‘)elif today == ‘Thursday‘:    print(‘上班‘)elif today == ‘Friday‘:    print(‘上班‘)elif today == ‘Saturday‘:    print(‘出去浪‘)elif today == ‘Sunday‘:    print(‘出去浪‘)else:    print(‘‘‘必須輸入其中一種:    Monday    Tuesday    Wednesday    Thursday    Friday    Saturday    Sunday    ‘‘‘)#方式二:today=input(‘>>: ‘)if today == ‘Saturday‘ or today == ‘Sunday‘:    print(‘出去浪‘)elif today == ‘Monday‘ or today == ‘Tuesday‘ or today == ‘Wednesday‘     or today == ‘Thursday‘ or today == ‘Friday‘:    print(‘上班‘)else:    print(‘‘‘必須輸入其中一種:    Monday    Tuesday    Wednesday    Thursday    Friday    Saturday    Sunday    ‘‘‘)#方式三:today=input(‘>>: ‘)if today in [‘Saturday‘,‘Sunday‘]:    print(‘出去浪‘)elif today in [‘Monday‘,‘Tuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘]:    print(‘上班‘)else:    print(‘‘‘必須輸入其中一種:    Monday    Tuesday    Wednesday    Thursday    Friday    Saturday    Sunday    ‘‘‘)練習三

  

十三 流程式控制制之while迴圈
1 為何要用迴圈

上節課我們已經學會用if .. else 來猜年齡的遊戲啦,但是只能猜一次就中的機率太小了,如果我想給玩家3次機會呢?就是程式啟動後,玩家最多可以試3次,這個怎麼弄呢?你總不會想著把代碼複製3次吧。。。。

age_of_oldboy = 48

guess = int(input(">>:"))

if guess > age_of_oldboy : 
print("猜的太大了,往小裡試試…")

elif guess < age_of_oldboy : 
print("猜的太小了,往大裡試試…")

else: 
print("恭喜你,猜對了…")

第2次 
guess = int(input(">>:"))

if guess > age_of_oldboy : 
print("猜的太大了,往小裡試試…")

elif guess < age_of_oldboy : 
print("猜的太小了,往大裡試試…")

else: 
print("恭喜你,猜對了…")

第3次 
guess = int(input(">>:"))

if guess > age_of_oldboy : 
print("猜的太大了,往小裡試試…")

elif guess < age_of_oldboy : 
print("猜的太小了,往大裡試試…")

else: 
print("恭喜你,猜對了…")

即使是小白的你,也覺得的太low了是不是,以後要修改功能還得修改3次,因此記住,寫重複的代碼是程式員最不恥的行為。 
那麼如何做到不用寫重複代碼又能讓程式重複一段代碼多次呢? 迴圈語句就派上用場啦

2 條件迴圈:while,文法如下

while 條件: 
迴圈體

 如果條件為真,那麼迴圈體則執行,執行完畢後再次迴圈,重新判斷條件。。。 如果條件為假,那麼迴圈體不執行,迴圈終止

列印0-10 
count=0 
while count <= 10: 
print(‘loop‘,count) 
count+=1

列印0-10之間的偶數 
count=0 
while count <= 10: 
if count%2 == 0: 
print(‘loop‘,count) 
count+=1

列印0-10之間的奇數 
count=0 
while count <= 10: 
if count%2 == 1: 
print(‘loop‘,count) 
count+=1

3 死迴圈

import time 
num=0 
while True: 
print(‘count‘,num) 
time.sleep(1) 
num+=1   
4 迴圈嵌套與tag

  tag=True

  while tag:

    ……

    while tag:

      ……..

      while tag:

        tag=False

練習,要求如下: 
1 迴圈驗證使用者輸入的使用者名稱與密碼 
2 認證通過後,運行使用者重複執行命令 
3 當使用者輸入命令為quit時,則退出整個程式

實現一: 
name=‘egon‘ 
password=‘123‘

while True: 
inp_name=input(‘使用者名稱: ‘) 
inp_pwd=input(‘密碼: ‘) 
if inp_name == name and inp_pwd == password: 
while True: 
cmd=input(‘>>: ‘) 
if not cmd:continue 
if cmd == ‘quit‘: 
break 
print(‘run <%s>‘ %cmd) 
else: 
print(‘使用者名稱或密碼錯誤‘) 
continue 
break

實現二:使用tag 
name=‘egon‘ 
password=‘123‘

tag=True 
while tag: 
inp_name=input(‘使用者名稱: ‘) 
inp_pwd=input(‘密碼: ‘) 
if inp_name == name and inp_pwd == password: 
while tag: 
cmd=input(‘>>: ‘) 
if not cmd:continue 
if cmd == ‘quit‘: 
tag=False 
continue 
print(‘run <%s>‘ %cmd) 
else: 
print(‘使用者名稱或密碼錯誤‘)

4 break與continue

break用於退出本層迴圈 
while True: 
print "123" 
break 
print "456"

continue用於退出本次迴圈,繼續下一次迴圈 
while True: 
print "123" 
continue 
print "456"

5 while+else

與其它語言else 一般只與if 搭配不同,在Python 中還有個while …else 語句,while 後面的else 作用是指,當while 迴圈正常執行完,中間沒有被break 中止的話,就會執行else後面的語句 
count = 0 
while count <= 5 : 
count += 1 
print("Loop",count)

else: 
print("迴圈正常執行完啦") 
print("-----out of while loop ------") 
輸出 
Loop 1 
Loop 2 
Loop 3 
Loop 4 
Loop 5 
Loop 6 
迴圈正常執行完啦 
-----out of while loop ------

如果執行過程中被break啦,就不會執行else的語句啦 
count = 0 
while count <= 5 : 
count += 1 
if count == 3:break 
print("Loop",count)

else: 
print("迴圈正常執行完啦") 
print("-----out of while loop ------") 
輸出

Loop 1 
Loop 2 
-----out of while loop ------

6 while迴圈練習題
  1. 使用while迴圈輸出1 2 3 4 5 6 8 9 10
  2. 求1-100的所有數的和
  3. 輸出 1-100 內的所有奇數
  4. 輸出 1-100 內的所有偶數
  5. 求1-2+3-4+5 … 99的所有數的和
  6. 使用者登陸(三次機會重試) 
    7:猜年齡遊戲 
    要求: 
    允許使用者最多嘗試3次,3次都沒猜對的話,就直接退出,如果猜對了,列印恭喜資訊並退出 
    8:猜年齡遊戲升級版 
    要求: 
    允許使用者最多嘗試3次 
    每嘗試3次後,如果還沒猜對,就問使用者是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往複,如果回答N或n,就退出程式 
    如何猜對了,就直接退出

題一 
count=1 
while count <= 10: 
if count == 7: 
count+=1 
continue 
print(count) 
count+=1

count=1 
while count <= 10: 
if count != 7: 
print(count) 
count+=1

題目二 
res=0 
count=1 
while count <= 100: 
res+=count 
count+=1 
print(res)

題目三 
count=1 
while count <= 100: 
if count%2 != 0: 
print(count) 
count+=1

題目四 
count=1 
while count <= 100: 
if count%2 == 0: 
print(count) 
count+=1

題目五 
res=0 
count=1 
while count <= 5: 
if count%2 == 0: 
res-=count 
else: 
res+=count 
count+=1 
print(res)

題目六 
count=0 
while count < 3: 
name=input(‘請輸入使用者名稱:‘) 
password=input(‘請輸入密碼:‘) 
if name == ‘egon‘ and password == ‘123‘: 
print(‘login success‘) 
break 
else: 
print(‘使用者名稱或者密碼錯誤‘) 
count+=1

題目七 
age_of_oldboy=73

count=0 
while count < 3: 
guess=int(input(‘>>: ‘)) 
if guess == age_of_oldboy: 
print(‘you got it‘) 
break 
count+=1

題目八 
age_of_oldboy=73

count=0 
while True: 
if count == 3: 
choice=input(‘繼續(Y/N?)>>: ‘) 
if choice == ‘Y‘ or choice == ‘y‘: 
count=0 
else: 
break

guess=int(input(‘>>: ‘))if guess == age_of_oldboy:    print(‘you got it‘)    breakcount+=1
十四 流程式控制制之for迴圈

1 迭代式迴圈:for,文法如下 
  for i in range(10): 
    縮排的代碼塊 
2 break與continue(同上) 
3 迴圈嵌套 
for i in range(1,10): 
for j in range(1,i+1): 
print(‘%s%s=%s‘ %(i,j,ij),end=‘ ‘) 
print() 
for+else

python全棧開發【第四篇】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.