Discuss / Python / 呜呜好难

呜呜好难

Topic source

sober°_

#1 Created at ... [Delete] [Delete and Lock User]
# 利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。from functools import reducedef guifan(s):    return s[0].upper() + s[1:].lower()def normalize(name):    name = map(guifan,name)    return nameL1 = ['adam', 'LISA', 'barT']L2 = list(normalize(L1))print(L2)# 作业2def complex(x,y):    return x * ydef prod(lst):    return reduce(complex,lst)print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))if prod([3, 5, 7, 9]) == 945:    print('测试成功!')else:    print('测试失败!')# 作业3# 字符串变为浮点数def complex(x,y):    return int(x) * 10 + int(y)def str2float(s):    L = list(s)    idx = L.index('.')    L1 = L[:idx]  # 整数    L2 = L[idx+1:]  # 小数    f = reduce(complex,L1)    b = reduce(complex,L2)    return f + b /10**len(L2)  #小数用整数除长度print('str2float(\'123.456\') =', str2float('123.456'))if abs(str2float('123.456') - 123.456) < 0.00001:    print('测试成功!')else:    print('测试失败!')

  • 1

Reply