python学习(11)———判断语句与循环语句

来源:互联网 发布:php语言精粹 编辑:程序博客网 时间:2024/06/03 14:07

判断语句

在python里面,判断语句主要是if,elif和else。这三种语句的判断方式,我们先从if开始。

if语句

if语句是很简单的,它只需要判断当前状态值与需要值的比较。如果达到条件,则进行相应的操作。这里我们举一个游戏的例子,一个人在体力小于20的时候为濒死状态。

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

得到的结果:
这里写图片描述
可以通过判断后,直接输出它要死了。

if-elif-else语句

扩展以上例子,如果它体力在20-80之间属于体力不健康,而80以上体力健康,我们该如何做。

power=10if 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." % bearelif 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." % ielements = []#用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.pythis 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 listadding 1 to the listadding 2 to the listadding 3 to the listadding 4 to the listadding 5 to the listElement Was: 0.Element Was: 1.Element Was: 2.Element Was: 3.Element Was: 4.Element Was: 5.

while语句

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

得到的结果:

D:\pystudy>python ex11_5.pythis 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 .
0 0