Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5469
  • 博文数量: 3
  • 博客积分: 1465
  • 博客等级: 上尉
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-14 17:02
文章分类

全部博文(3)

文章存档

2011年(1)

2010年(2)

我的朋友
最近访客

分类: Python/Ruby

2010-08-19 11:05:47

前面看了for语句,现在看while语句。
while需要一个条件为真的情况下,重复执行语句,还用上面的例子,当输入数字为23时,执行结束。
eg.
 
#!/usr/bin/python
# Filename: while.py

number = 23
running = True

while running:
    guess = int(raw_input('Enter an integer : '))

    if guess == number:
        print 'Congratulations, you guessed it.'
        running = False # this causes the while loop to stop
    elif guess < number:
        print 'No, it is a little higher than that'
    else:
        print 'No, it is a little lower than that'
else:
    print 'The while loop is over.'
    # Do anything else you want to do here

print 'Done'
 
 
运行结果:python while.py
Enter an integer : 12
No, it is a little higher than that
Enter an integer : 455
No, it is a little lower than that
Enter an integer : 32
No, it is a little lower than that
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
 
Ps:while循环中使用一个else从句
阅读(417) | 评论(0) | 转发(0) |
0

上一篇:python 学习

下一篇:CU改版

给主人留下些什么吧!~~