Python 学习笔记(二)
上一篇 /
下一篇 2010-08-10 14:18:23
/ 个人分类:python
Python 学习笔记(二)
昨天实践了if...else语句,今天继续实践while语句.
今天执行例子时,照样遇到了困难,不过我已经习惯了,我的目标就是扫除一切问题,让代码能够运行.
实践步骤:
(这个例子是《简明 Python 教程》中,用来说明while用法的)
打开:IDLE;
输入:
>>> number = 23
>>> running = True
>>> while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False
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.'
print 'Done'
Enter an integer : 10
No, it is a little higher than that
Enter an integer : 30
No, it is a little lower than that
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
>>>
总结:
1.布尔类型值,第一个字母要大写,如:True,False。
2.这个例子的好处在于,只要我们没有输入正确的guess值,就一直可以输入,不需要重复执行程序,原因在于:判断条件为假时,一直需要循环。
3.print 'Done',在例子中,该项是跟while一个级别的,但实际上它应该放在else中。
收藏
举报
TAG: