Python之函数作业

Python之函数作业

   爬页面

 

#爬虫页面,send一次爬一次


from urllib.request import urlopen

def get():
    while True:
        url = yield
        res = urlopen(url).read()
        print(res)


g=get()
next(g)

g.send("http://www.baidu.com")

 

 

 

1、8<<2等于?

2、通过内置函数计算5除以2的余数

3、s=[1,"h",2,"e",[1,2,3],"l",(4,5),"l",{1:"111"},"o"],将s中的5个字符提取出来并拼接成字符串。

4、判断"yuan"是否在[123,(1,"yuan"),{"yuan":"handsome"},"yuanhao"],如何判断以及对应结果?

5、l=[1,2,3]
l2=l.insert(3,"hello")
print(l2)
执行结果并解释为什么?

6、 a=[1,2,[3,"hello"],{"egon":"aigan"}]
b=a[:]

a[0]=5
a[2][0]=666

print(a)
print(b)
#计算结果以及为什么?

7 使用文件读取,找出文件中最长的行的长度(用一行代码解决)?

8 def add(s, x):
return s + x

def generator():
for i in range(4):
yield i

base = generator()
for n in [1, 11]:
base = (add(i, n) for i in base)

print list(base)

9
hello.py (gbk方式保存):
#coding:GBK
print(“老男孩”)

如果用py2,py3下在cmd下运行回报错吗?为什么并提出解决方案? (编码)

10 通过函数化编程实现5的阶乘
11 打印如下图案:


***
*****
*******
*****
***
*

12
def outer():
count = 10
def inner():
count = 20
print(count)
inner()
print(count)
outer()

(1)分析运行结果?
(2)如何让两个打印都是20

13 输入一个年份,判断是否是闰年?

14 任意输入三个数,判断大小?

15 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222
,几个数相加以及a的值由键盘控制。

16 f=open("a")

while 1:
choice=input("是否显示:[Y/N]:")
if choice.upper()=="Y":
for i in f:
print(i)
else:
break

请问程序有无bug,怎么解决?


17

def foo():
print('hello foo')
return()
def bar():
print('hello bar')


(1)为这些基础函数加一个装饰器,执行对应函数内容后,将当前时间写入一个文件做一个日志记录。
(2)改成参数装饰器,即可以根据调用时传的参数决定是否记录时间,比如@logger(True)

18 三次登陆锁定:要求一个用户名密码输入密码错误次数超过三次锁定?

posted @ 2018-05-28 22:04  王先生是胖子  阅读(344)  评论(0编辑  收藏  举报