Discuss / Python / 作业提交,学习python第三天,加油!!!

作业提交,学习python第三天,加油!!!

Topic source

郝仁E哥

#1 Created at ... [Delete] [Delete and Lock User]

#练习1 print('----------分割线1---------------') def normalize(name): return name[0].upper() +name[1::].lower()

print(normalize('hELlo'))

测试:

L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

#练习2 print('----------分割线2---------------') def prod(L): return reduce(lambda x,y:x*y,L)

print('3 5 7 * 9 =', prod([3, 5, 7, 9])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')

#练习3 print('----------分割线3---------------') def str2float(s): DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def char2num(s): return DIGITS[s] s=s.split('.') #split('.')将字符串按‘.’分隔开 n=s[0]+s[1] #连接两个字符串,整体转换 return reduce(lambda x,y:x10+y,map(char2num,n))/(10*len(s[1]))

print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')

10**len(s[1])

郝仁E哥

#3 Created at ... [Delete] [Delete and Lock User]

评论的显示少了个'',我自己的代码里是有‘**’,评论板显示有点问题,不过谢谢指正了


  • 1

Reply