python2000个5除以84的余数_Python基础-月考

1. 8<<2等于?

1 #32

2 #解释:将8按位左移2位

3 #8 0 0 0 0 1 0 0 0

4 #32 0 0 1 0 0 0 0 0

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

1 print(divmod(5, 2))2

3 #(2, 1) 2 = 5 // 2; 1 = 5 % 2

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

1 s = [1, "h", 2, "e", [1, 2, 3], "l", (4, 5), "l", {1: "111"}, "o"]2 s_g = filter(lambda x: type(x) is str, s) #由于int类型非可迭代对象,先使用filter过滤掉

3 s_new = ‘‘ #拼接字符串变量

4 for i ins_g:5 s_new +=i6 print(s_new)

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

1 l = [123, (1, "yuan"), {"yuan": "handsome"}, "yuanhao"]2 l_yuan = str(l) #将列表转换为字符串

3 if ‘yuan‘ inl_yuan:4 print(‘True‘)

5. 执行结果并解释为什么?

l=[1,2,3]

l2=l.insert(3,"hello")

print(l2)

1 执行结果:None2 解释:‘.insert’方法的返回值为‘None’

6. 计算结果以及为什么?

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

b=a[:]

a[0]=5

a[2][0]=666

print(a)

print(b)

1 a = [1,2,[3,"hello"],{"egon":"aigan"}]2 b = a[:] #列表b和列表a相同

3

4 a[0] = 5 #将列表a中的第一个元素‘1’变为‘5’

5 a[2][0] = 666 #将列表a中的第3个元素‘[3,"hello"]’(同样为列表)的第1个元素变为‘666’

6

7 print(a) #打印更改后的列表a

8 print(b) #打印列表b,就是列表a更改前的元素

9

10 #执行结果: [5, 2, [666, ‘hello‘], {‘egon‘: ‘aigan‘}]

11 #[1, 2, [666, ‘hello‘], {‘egon‘: ‘aigan‘}]

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

1 print(max([len(x) for x in open(‘test.py‘, encoding=‘utf-8‘)]))

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))

1 #结果:[22, 23, 24 ,25]

2 #解释:

3 #这个可以以管道的思路来理解,首先generator()函数是第一个生成器,下一个是第一次循环的base = (add(i, n) for i in base),

4 #最后一个生成器是第二次循环的base = (add(i, n) for i in base)。 这样就相当于三个管道依次连接,但是水(数据)还没有流过,

5 #现在到了list(base),就相当于驱动器,打开了水的开关,这时候,按照管道的顺序,由第一个产生一个数据,yield 0,

6 #然后第一个管道关闭。 之后传递给第二个管道就是第一次循环,此时执行了add(0, 11),然后水继续流,

7 #到第二次循环,再执行add(11, 11),此时到管道尾巴了,第一个数据22就产生了。此时第一个管道再开放yield 1,

8 #流程跟上面的一样。依次产生23,24,25; 直到没有数据。 把代码改一下容易理解:

9

10 defgenerator():11 for i in range(4):12 yield i #第1个管道

13

14 base = (add(i, n) for i in base) #第2个管道

15 base = (add(i, n) for i in base) #第3个管道

16

17 list(base) #开关驱动器

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

test.py (gbk方式保存):

#coding:GBK

print(“老男孩”)

扩展,但是在python2.x解释器中,会显式乱码,python3.x下正常显式,是因为解释器自动帮我们解码了文件内容:

10. 通过函数化编程实现5的阶乘

1 deffn(n):2 x = 1

3 if n == 0: #0的阶乘为1

4 x5 else:6 for i in range(1, n+1):7 x *=i8 returnx9

10 print(fn(5))

11. 打印如下图案:

*

***

*****

*******

*****

***

*

1 def print_star(n): #n必须为奇数,此程序没有做判断

2 s = ‘*‘

3 i = 1 #上部分,包含对称行

4 j = n #下部分

5 while i < n and i % 2 !=0:6 print((s *i).center(n))7 i += 2

8 while j > 0 and j % 2 !=0:9 print((s *j).center(n))10 j -= 2

11

12 print_star(7)

12

def outer():

count = 10

def inner():

count = 20

print(count)

return count

inner()

print(count)

outer()

(1). 分析运行结果?

1 运行结果:20

2 10

3 分析:当调用函数outer时,按顺序读函数定义部分,outer函数内部嵌套定义了inner函数,4 读完inner函数的定义,首先调用了inner(),此时inner函数局部变量count为20,故先打印了20,然后打印了outer函数内部变量10

(2).如何让两个打印都是20

1 #方法1:将inner函数内部的count变量删除,将outer函数下的count变量赋值为20即可

2 defouter():3 count = 20

4 definner():5 print(count)6 returncount7 inner()8 print(count)9

10 outer()

1 #方法2:将inner函数内部变量count = 20返回给外部函数outer的变量count

2 defouter():3 definner():4 count = 20

5 print(count)6 returncount7 count =inner()8 print(count)9 outer()

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

1 #闰年:四年一润,百年不润,四百年再润

2 defleap_year(y):3 if y % 100 ==0:4 if y % 400 ==0:5 print(‘leap year‘)6 else:7 print(‘not leap year‘)8 elif y % 4 ==0:9 print(‘leap year‘)10 else:11 print(‘not leap year‘)12

13 leap_year(2000)

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

1 defmax_three(x, y, z):2 returnmax(max(x, y),z)3

4 print(max_three(1,2,3))

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

1 def sum_new(a, n): #a代表求和的数字,n代表相加的次数

2 sum =03 a =str(a)4 for i in range(1, n+1):5 x = int(a *i)6 sum +=x7 returnsum8

9 print(sum_new(2,3))

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

1 f=open("a")2 while 1:3 choice=input("是否显示:[Y/N]:")4 if choice.upper()=="Y":5 for i inf:6 print(i)7 else:8 break

1 #有bug,

2 #1,只要用户输入非‘y/Y’字符,程序都会结束,这与提示信息不符合

3 #2. ‘y/Y’只一次有效,如果用户想再次查看文件内容,必须得重启程序

4 #解决方法:

5 f = open("a")6 while 1:7 choice=input("是否显示:[Y/N]:")8 if choice.upper() == "Y":9 for i inf:10 print(i)11 f.seek(0)  #重置文件位置于文首12 elif choice.upper() == "N":  #严格要求用户必须输入‘n/N’方可退出程序13 break

17.

1 deffoo():2 print(‘hello foo‘)3 return()4 defbar():5 print(‘hello bar‘)

(1). 为这些基础函数加一个装饰器,执行对应函数内容后,将当前时间写入一个文件做一个日志记录。

1 deftimer(func):2 defwrapper():3 importtime4 res =func()5 f = open(‘log‘, ‘a+‘) #以追加的方式打开文件,没有则会创建

6 s = time.asctime() #获取当前时间:Tue Apr 18 21:46:18 2017

7 f.write(s + ‘\n‘) #将当前时间写入log文件,并换行

8 f.close() #关闭log文件

9 returnres10 returnwrapper11

12 @timer13 deffoo():14 print(‘hello foo‘)15 return()16 @timer17 defbar():18 print(‘hello bar‘)19

20 foo()21

22 bar()

(2). 改成参数装饰器,即可以根据调用时传的参数决定是否记录时间,比如@logger(True)

1 deflogger(choice):2 deftimmer(func):3 defwrapper():4 importtime5 if choice ==True:6 res =func()7 f = open(‘log‘, ‘a+‘) #以追加的方式打开文件,没有则会创建

8 s = time.asctime() #获取当前时间:Tue Apr 18 21:46:18 2017

9 f.write(s + ‘\n‘) #将当前时间写入log文件,并换行

10 f.close() #关闭log文件

11 returnres12 else:13 pass

14 returnwrapper15 returntimmer16

17 @logger(True)18 deffoo():19 print(‘hello foo‘)20 return()21 @logger(True)22 defbar():23 print(‘hello bar‘)24

25 foo()26

27 bar()

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

1 with open(‘account‘,encoding=‘utf8‘) as f_account, open(‘lockedlist‘, ‘a+‘) as f_locked:2 l = [] #定义用户名验证列表,存放黑名单数据

3 f_locked.seek(0) #"a+"模式打开后,文件位置位于末尾,要遍历文件内容,需要将指针移至文件起始位置

4 for locked_info in f_locked.readlines(): #遍历黑名单

5 l.append(locked_info.strip()) #将黑名单数据添加进列表,注意:需要将文件中的换行符脱掉

6

7 c = [] #定义用户登录名列表,存储用户尝试的登录名

8 count = 1 #登陆次数计数器

9 flag = True #登陆循环控制开关

10 while flag and count < 4:11 user = input(‘Please input username:‘) #输入用户名

12 pwd = input(‘Please input password:‘) #输入用户密码

13 if user in l: #用户名在黑名单中

14 print("This user is in blacklist,can‘t log in!") #打印提示信息

15 continue

16 c.append(user)17 for info in f_account: #用户名不在黑名单中,遍历用户登陆文件

18 user_name, user_pwd = info.strip().split(‘,‘) #将文件中的用户名和登陆密码赋值给判定变量

19 if user == user_name: #用户名符合

20 if pwd == user_pwd: #对应密码符合

21 print(‘Welcome %s‘ % user) #打印登陆成功信息

22 flag = False #登陆成功,跳出登陆程序

23 break

24 count += 1 #对应密码不符合,while循环计数器加1

25 break

26 count += 1 #用户名不符合,while循环计数器加1

27 break

28 if count == 4: #如果不同用户名密码输错3次,关闭程序

29 print(‘More than 3 times wrong!‘) #打印提示信息

30 if len(c) == 3 and c[0] == c[1] == c[2]: #如果相同用户名密码输错3次,将该用户名加入黑名单,限制登陆

31 print(‘This user has been locked!‘)32 f_locked.write(user+‘\n‘) #将错误3次的用户名写入黑名单,注意,加换行符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值