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 .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值