���� 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ѭ�� |