全部博文(3)
分类: LINUX
2010-08-19 10:57:51
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'
运行结果:
[root@www jerry]# python if.py
Enter an integer : a
Traceback (most recent call last):
File "for.py", line 5, in ?
guess = int(raw_input('Enter an integer : '))
ValueError: invalid literal for int(): a
[root@www jerry]# python if.py
Enter an integer : 12
No, it is a little higher than that
Done
[root@www jerry]# python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
[root@www jerry]# python if.py
Enter an integer : 24
No, it is a little lower than that
Done
[root@www jerry]#
但是如果去掉类型int呢?
guess =
int
(
raw_input
(
'Enter an integer : '
)) 改成guess =
raw_input
(
'Enter an integer : '
)
运行结果:
[root@www jerry]# python if.py
Enter an integer : a
No, it is a little lower than that
Done
[root@www jerry]#
PS:在Python中没有
switch
语句。你可以使用if..elif..else
语句来完成同样的工作