python學習(11)———判斷語句與循環語句

判斷語句

在python裏面,判斷語句主要是if,elif和else。這三種語句的判斷方式,我們先從if開始。

if語句

if語句是很簡單的,它只需要判斷當前狀態值與需要值的比較。如果達到條件,則進行相應的操作。這裏我們舉一個遊戲的例子,一個人在體力小於20的時候爲瀕死狀態。

power=10
if(power<20):
    print "Is going to die"

得到的結果:
這裏寫圖片描述
可以通過判斷後,直接輸出它要死了。

if-elif-else語句

擴展以上例子,如果它體力在20-80之間屬於體力不健康,而80以上體力健康,我們該如何做。

power=10
if power < 20 :
    print "Is going to die!"
elif power < 80 :
    print "power is not healthy!"
else:
    print "power is healthy"

這裏就不發表結果了,不同的blood值可以得到不同的結果。

加入輸入來做決定

我們再重新編寫一個例子,假設在黑暗的房間裏面有兩道門。你不知道這兩道門裏有什麼。然後你進入其中一個門。這兩個門分別由1號門裏是一個巨大的熊仔吃奶油蛋糕,你可以選擇拿走蛋糕或者對熊尖叫。如果拿走蛋糕,熊就會吃掉你的臉,如果你對熊尖叫,熊就回吃掉你的腿,如果什麼都不做,熊就跑了。然後2號門裏,你就陷入另一個怪圈。你可以選擇藍莓、黃色夾克、以及去聽左輪手槍開槍的聲音。

print "You enter a dark room with two doors.  Do you go through door #1 or door #2?"

door = raw_input("> ")

if door == "1":
    print "There's a giant bear here eating a cheese cake.  What do you do?"
    print "1. Take the cake."
    print "2. Scream at the bear."

    bear = raw_input("> ")

    if bear == "1":
        print "The bear eats your face off.  Good job!"
    elif bear == "2":
        print "The bear eats your legs off.  Good job!"
    else:
        print "Well, doing %s is probably better.  Bear runs away." % bear

elif door == "2":
    print "You stare into the endless abyss at Cthulhu's retina."
    print "1. Blueberries."
    print "2. Yellow jacket clothespins."
    print "3. Understanding revolvers yelling melodies."

    insanity = raw_input("> ")

    if insanity == "1" or insanity == "2":
        print "Your body survives powered by a mind of jello.  Good job!"
    else:
        print "The insanity rots your eyes into a pool of muck.  Good job!"

else:
    print "You stumble around and fall on a knife and die.  Good job!"

循環語句

for語句

這節,我們來了解循環語句,循環語句主要以for爲代表,它的展現形式如下:

# -*- coding: utf-8 -*-
nums = [1, 2, 3, 4, 5] 
animals = ["chicken", "wolf", "lion", "monkey", "pig"]
change = [1, 'haha', 2, 'nini', 3, 'xixi']

#輸出數組中的每一個元素
for num in nums:
    print "this is number %d." % num

#輸出字符串組中的每一個字符串 
for animal in animals:
    print "The animal is %s." % animal

#輸出列表中的每一個元素
for i in change:
    print "The element is %r." % i

elements = []

#用range函數從0到5計數
for i in range(0,6):
    print "adding %d to the list" % i
    elements.append(i)

#輸出elements裏面的值
for i in elements:
    print "Element Was: %d." %i

得到的結果:

D:\pystudy>python ex11_4.py
this is number 1.
this is number 2.
this is number 3.
this is number 4.
this is number 5.
The animal is chicken.
The animal is wolf.
The animal is lion.
The animal is monkey.
The animal is pig.
The element is 1.
The element is 'haha'.
The element is 2.
The element is 'nini'.
The element is 3.
The element is 'xixi'.
adding 0 to the list
adding 1 to the list
adding 2 to the list
adding 3 to the list
adding 4 to the list
adding 5 to the list
Element Was: 0.
Element Was: 1.
Element Was: 2.
Element Was: 3.
Element Was: 4.
Element Was: 5.

while語句

i =6
while i > 0:
    print("this is the line : %d .") % i
    i=i-1

得到的結果:

D:\pystudy>python ex11_5.py
this is the line : 6 .
this is the line : 5 .
this is the line : 4 .
this is the line : 3 .
this is the line : 2 .
this is the line : 1 .
發佈了21 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章