易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 :

Python入门的36个例子 之 32

源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as salary, courses and leaves for teachers and,
marks and fees for students.
You can create two independent classes for each type and process them
but adding a new common characteristic would mean adding to both of
these independent classes. This quickly becomes unwieldy.
A better way would be to create a common class called SchoolMember and
then have the teacher and student classes inherit from this class i.e.
they will become sub-types of this type (class) and then we can add
specific characteristics to these sub-types.
这段文字很简单,我开始也这么认为。但当我不小心第二次读这本书的时候才领会到这些文字所要传达的深意。
继承在我看来,或者说在以前的我看来,是为了代码重用,是的,我现在也这么 ......

Python入门的36个例子 之 33

源代码下载:下载地址在这里
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
# end of try and except
f.close()
# end of while

output:
Input a file name:haha
There is no file named haha
Input a file name:txt
There is no file named txt
Input a file name:1
There is no file named 1
Input a file name:037_Exception.py
Opened a file.
Input a file name:q ......

Python入门的36个例子 之 34

源代码下载:下载地址在这里
raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# end of def
print getAge()

output:
以下是两次运行的结构
>>>
Input your age:22
22
>>>
Input your age:911
Traceback (most recent call last):
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 11, in <module>
    print getAge()
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 6, in getAge
    raise 'BadAgeError', 'It is impossible!!!!!'
TypeError: exceptions must be classes or instances, not str
>>> ......

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个例子 之 36

# 040
import time
try:
f = file('040_Finally.py')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(0.33)
print line,
# end of while
finally:
f.close()
print 'Closed the file.'
# end of try
output:
>>>
# 040
import time
try:
    f = file('040_Finally.py')
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        time.sleep(0.33)
        print line,
    # end of while
finally:
    f.close()
    print 'Closed the file.'
# end of try
   
Closed the file.
>>>
源代码下载:下载地址在这里
......

穿越Python Challenge

 第九关 Image
    从页面上的图片可以看到有一串点,那么是不是代表该关与图像点有关? 我们从页面源码可以看到,有两段数字序列first和second,而有一个提示first+second=? 什么意思呢?难道是说(first, second)代表了图像点的坐标?不像,两段序列的长度有很大差异。那么算符+还有什么含义呢,有可能是将两段序列拼起来,然后每两个数字代表一个图像点。通过处理,我们在原图片上按照给定的坐标涂黑点,却发现什么都看不清;因此我们按照图片的规格新建一个图片,在黑底上涂白点,处理程序如下:
#!/bin/python
# file: good.py

import re, Image

file = open('d:\Python\good.html')
message = re.findall('(<!--[^-]+-->)', file.read(), re.S)[1]
file.close()

first = re.findall('(\d+)',re.findall('first:(.+)second',message,re.S)[0],re.S)
second = re.findall('(\d+)',re.findall('second:(.+)>',message,re.S)[0],re.S)

all = first + second

im = Image.open('d:\Python\good.jpg')
im2 = Image.new(im.mode, im.size)

for x in range(0,len(all),2):
im2.putpixel((int(all[x]),int(a ......
总记录数:40319; 总页数:6720; 每页6 条; 首页 上一页 [6614] [6615] [6616] [6617] 6618 [6619] [6620] [6621] [6622] [6623]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号