易截截图软件、单文件、免安装、纯绿色、仅160KB

Python入门的36个例子 之 35

源代码下载:下载地址在这里
# 039
while True:
try:
x = int(raw_input('Input a number:'))
y = int(raw_input('Input a number:'))
z = x / y
except ValueError, ev:
print 'That is not a valid number.', ev
except ZeroDivisionError, ez:
print 'Divisor is zero:', ez
except:
print 'Unexpected error.'
raise
else:
print 'There is no error.'
break
# end of try
# end of while
print x,'/',y,'=',x/y

output:
Input a number:w
That is not a valid number. invalid literal for int() with base 10: 'w'
Input a number:9
Input a number:0
Divisor is zero: integer division or modulo by zero
Input a number:9
Input a number:3
There is no error.
9 / 3 = 3


相关文档:

Python入门的36个例子 之 19

源代码下载:下载地址在这里
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# 列表的合并
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# 判断列表中是否包含某元素
print 'b' in list1
print 1 in list1
# 删除某个元素
for ele in ......

Python入门的36个例子 之 23

源代码下载:下载地址在这里
# 026
aList = ['1','2','3','4']
aListCopy = aList # 其实,这里仅仅复制了一个引用
del aList[0]
print aList
print aListCopy # 两个引用指向了了同一个对象,所以打印结果一样
aListCopy = aList[:] # 这是复制整个对象的有效方法
del aList[0]
print aList
print aListCopy
......

Python入门的36个例子 之 26

源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()

output:
>>>
This is the first line.
This is the second line.
This is ......

Python入门的36个例子 之 29

源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()

output:
22
Hello! ......

Python入门的36个例子 之 30

源代码下载:下载地址在这里
# 034
class Person:
def __init__(self, name):
self.name = name # 这一句中第一个name是类中包含的域,第二个name是传进来的参数
# end of def
def sayHello(self):
print 'Hello!'
# end of def
# end of class
p = Person('Ning')
print p.nam ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号