| ���� Python �̳� | ��6�� ������ | |
|---|---|---|
| ��һҳ | while��� | ��һҳ |
ֻҪ��һ������Ϊ�������£�while����������ظ�ִ��һ����䡣while�������ν ѭ�� ����һ�����ӡ�while�����һ����ѡ��else�Ӿ䡣
#!/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'
��Դ�ļ���code/while.py��
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
����������У�������Ȼʹ���˲�����Ϸ��Ϊ���ӣ�����������ӵ����������û����Բ��ϵIJ�����ֱ�����¶�Ϊֹ���������Ͳ���Ҫ��ǰ���Ǹ���������Ϊÿ�β²��ظ�ִ��һ������������ǡ����˵����while����ʹ�á�
���ǰ�raw_input��if����Ƶ���whileѭ���ڣ�������whileѭ����ʼǰ��running��������ΪTrue�����ȣ����Ǽ������running�Ƿ�ΪTrue��Ȼ��ִ�к���� while-�� ����ִ����������֮���ٴμ�������������������У�������running���������������ģ������ٴ�ִ��while-�飬�������Ǽ���ִ�п�ѡ��else-�飬������ִ����һ����䡣
��whileѭ��������ΪFalse��ʱ��else��ű�ִ�С���������Ҳ��������������һ�α������ʱ�����whileѭ����һ��else�Ӿ䣬����ʼ�ձ�ִ�У��������whileѭ������Զѭ����ȥ���������
True��False����Ϊ�������͡�����Էֱ�����ǵ�Ч������Ϊֵ1��0���ڼ�����Ҫ������ʱ��������ʮ����Ҫ�����Dz�������ʵ��ֵ1��
else����ʵ���Ƕ���ģ���Ϊ��������е�������ͬһ�飨��while��ͬ���У�����while���֮����������ȡ����ͬ��Ч����
��C/C++����Ա��ע��
��ס���������whileѭ����ʹ��һ��else�Ӿ䡣
| ��һҳ | ��һ�� | ��һҳ |
|---|---|---|
| if��� | ��ҳ | forѭ�� |