全部博文(3)
分类: Python/Ruby
2010-08-19 11:05:47
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'
运行结果:python while.py
Enter an integer : 12
No, it is a little higher than that
Enter an integer : 455
No, it is a little lower than that
Enter an integer : 32
No, it is a little lower than that
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
Ps:while循环中使用一个else
从句