| ���� Python �̳� | ��6�� ������ | |
|---|---|---|
| ��һҳ | if��� | ��һҳ |
if�����������һ�������� ��� ����Ϊ�棬��������һ����䣨��Ϊ if-�� ���� ���� ���Ǵ�������һ����䣨��Ϊ else-�� ���� else �Ӿ��ǿ�ѡ�ġ�
#!/usr/bin/python
# Filename: if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
print 'No, it is a little higher than that' # Another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that'
# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement is executed
��Դ�ļ���code/if.py��
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
����������У����Ǵ��û����õ��²������Ȼ�����������Ƿ����������е��Ǹ������ǰѱ���number����Ϊ������Ҫ���κ��������������������23��Ȼ������ʹ��raw_input()����ȡ���û��²�����֡�����ֻ�����õij���Ρ����ǽ�����һ��ѧϰ������ں�����֪ʶ��
����Ϊ�ڽ���raw_input�����ṩһ���ַ���������ַ�������ӡ����Ļ�ϣ�Ȼ��ȴ��û������롣һ����������һЩ������Ȼ���س���֮�����������롣����raw_input������˵��һ���ַ���������ͨ��int������ַ���ת��Ϊ�������������洢�ڱ���guess�С���ʵ�ϣ�int��һ���࣬���������ڶ��������˽��ֻ������һ���ַ���ת��Ϊһ����������������ַ�������һ����Ч�������ı���Ϣ����
�����������ǽ��û��IJ²�������ѡ��������Ƚϡ����������ȣ����Ǵ�ӡһ���ɹ�����Ϣ��ע������ʹ�����������������Pythonÿ�����ֱ�������һ���顣�����Ϊʲô������Python�����Ҫ��ԭ����ϣ�����ܹ���֡�ÿ��������һ���Ʊ������Ĺ���������������
ע��if����ڽ�β������һ��ð�š�������ͨ��������Python�������һ�����顣
Ȼ�����Ǽ���²��Ƿ�С�����ǵ���������������ģ����Ǹ����û����IJ²����һ�㡣����������ʹ�õ���elif�Ӿ䣬����ʵ�ϰ������������if else-if else���ϲ�Ϊһ��if-elif-else��䡣��ʹ�ó�����Ӽ����Ҽ��������������������
elif��else�Ӿ䶼���������н�β����һ��ð�ţ��������һ����Ӧ�����飨��Ȼ��������ȷ����������
��Ҳ������һ��if����ʹ������һ��if��䣬�ȵȡ����ⱻ��ΪǶ��if��䡣
��ס��elif��else�����ǿ�ѡ�ġ�һ�������Чif����ǣ�
if True:
print 'Yes, it is true'
��Pythonִ����һ��������if����Լ������������elif��else�Ӿ�֮��������if�������һ����䡣����������У�������������顣��������鿪ʼִ�У�����һ�������print 'Done'��䡣����֮��Python��������Ľ�β���Ľ������С�
��������һ���dz��ij��������Ѿ�������ij�����ָ����������Ӧ��ע��ĵط���������Щ����ʮ��ֱ���˵��ģ�������Щӵ��C/C++�������û���˵����Ϊ�ģ��������ڿ�ʼʱ���������ע�⣬�����Ժ��������Ǹе���Ϥ������Ȼ����
��C/C++����Ա��ע��
��Python��û��switch��䡣�����ʹ��if..elif..else��������ͬ���Ĺ�������ijЩ���ϣ�ʹ���ֵ�����ӿ�ݡ���
| ��һҳ | ��һ�� | ��һҳ |
|---|---|---|
| ��� | ��ҳ | while��� |