- 论坛徽章:
- 0
|
目前在学习sys module,程序版本是3.1,而看的书是关于python2.5的。
代码如下:
import sys
sum = 0
while True:
try:
line = input('--->') # or call sys.stdin.readlines( )
except EOFError: # or for line in sys.stdin:
break # raw_input strips \n at end
else:
sum += int(line) # was sting.atoi( ) in 2nd ed
print ( "sum = :",sum)
finally:
print (" the end",sum)
print (sum)
|
这是运行的效果:
>>>
--->3
sum = : 3
the end 3
--->4
sum = : 7
the end 7
--->5
sum = : 12
the end 12
--->6
sum = : 18
the end 18
--->
the end 18
Traceback (most recent call last):
File "D:/Python31/36sorter.py", line 9, in <module>
sum += int(line) # was sting.atoi( ) in 2nd ed
ValueError: invalid literal for int() with base 10: ''
>>>
运行时,为什么不出现EOF报错,而是 系统错误,最后的print语句总也出不来。 |
|