Python中控制語句__Python

來源:互聯網
上載者:User
淺析概念 while迴圈
import randomsecret=int(random.uniform(0,10))print("我想的數字在0到10之間,猜猜吧。")guess=11while guess!=secret:    guess=int(input("輸入猜的數字吧。"))print("Well Done!")

結果:

我想的數字在0到10之間,猜猜吧。輸入猜的數字吧。3輸入猜的數字吧。2輸入猜的數字吧。1輸入猜的數字吧。4輸入猜的數字吧。5輸入猜的數字吧。6輸入猜的數字吧。7輸入猜的數字吧。8輸入猜的數字吧。9輸入猜的數字吧。10輸入猜的數字吧。0Well Done!>>> 
for迴圈

下列例子都是合法的:

>>> for i in [1,2,3,4,5,6,7,8,9,10,11,12]:    print(i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 727 times 12 is 848 times 12 is 969 times 12 is 10810 times 12 is 12011 times 12 is 13212 times 12 is 144
>>> for i in (1,2,3,4,5,6):    print (i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 72
>>> for i in {1,2,3,4,5,6}:    print (i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 72
for i in "123456789":    print (i,"times 12 is",i*12)1 times 12 is 1111111111112 times 12 is 2222222222223 times 12 is 3333333333334 times 12 is 4444444444445 times 12 is 5555555555556 times 12 is 6666666666667 times 12 is 7777777777778 times 12 is 8888888888889 times 12 is 999999999999#上面這種方法雖然不報錯,但沒有得到我們想要的結果,它展示了使用錯誤資料時會出現什麼結果。
# 注意區分:# { } 在python中代表集合# [ ] 在python中代表列表# ( ) 在python中代表元組

特別的是,字典這種資料類型的處理與其他類型資料不太一樣

>>> real_name={"Benny":"Benjamin Everard","Alex":"Alexander Bradbury"}>>> for key,value in real_name.items():    print("The real name of "+ key+"is"+value)The real name of BennyisBenjamin EverardThe real name of AlexisAlexander Bradbury#for迴圈的查詢方式類似於SQL語句

值得注意的是:當使用集合時,就不能控制訪問資料的順序;一般情況下,如果使用for迴圈,需要使用列表或元組,而不是集合或者字典。 深入理解 尋找素數

包含迴圈的嵌套調用

>>> for i in range(1,100,2):    is_prime=True    for k in range(2,i):        if(i%k==0):            print(i,"is divisible by",k)            is_prime=False            break    if is_prime:        print(i,"is prime")1 is prime3 is prime5 is prime7 is prime9 is divisible by 311 is prime13 is prime15 is divisible by 317 is prime19 is prime21 is divisible by 323 is prime25 is divisible by 527 is divisible by 329 is prime31 is prime33 is divisible by 335 is divisible by 537 is prime39 is divisible by 341 is prime43 is prime45 is divisible by 347 is prime49 is divisible by 751 is divisible by 353 is prime55 is divisible by 557 is divisible by 359 is prime61 is prime63 is divisible by 365 is divisible by 567 is prime69 is divisible by 371 is prime73 is prime75 is divisible by 377 is divisible by 779 is prime81 is divisible by 383 is prime85 is divisible by 587 is divisible by 389 is prime91 is divisible by 793 is divisible by 395 is divisible by 597 is prime99 is divisible by 3
if語句
#一個if語句最多執行一段代碼(多個elif語句並列情況),例如以下例題,程式只判斷10和2的關係,不會再判斷10與5的關係。num=int(input("enter a number:"))if num%2==0:    print("Your number is divisible by 2")elif num%3==0:    print("Your number is divisible by 3")elif num%5==0:    print("Your number is divisible by 5")else:    print("Your number is not divisible by 2,3 or 5")

結果:

enter a number:10Your number is divisible by 2
捕獲異常

原理:python在執行程式出錯後會返回錯誤類型,藉此來實現排錯。

is_number=Falsenum=0while not is_number:    is_number=True    try:        num=int(input("enter a number"))    except ValueError:        print("i said a number!")        is_number=Falseif num%2==0:    print("Your number is divisible by 2")elif num%3==0:    print("Your number is divisible by 3")elif num%5==0:    print("Your number is divisible by 5")else:    print("Your number is not divisible by 2,3 or 5")

結果:

enter a number dashi said a number!enter a number3Your number is divisible by 3

練習:修改開始時的猜數字遊戲

import randomsecret=int(random.uniform(0,10))print("我想的數字在0到10之間,猜猜吧。")guess=11while guess!=secret:    try:        guess=int(input("Take a guess!"))    except ValueError:        print("i said a number!")print("Well Done!")

結果:

我想的數字在0到10之間,猜猜吧。Take a guess!di said a number!Take a guess!1Take a guess!2Take a guess!3Well Done!

聯繫我們

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