Discuss / Python / 自己写的作业,简单易懂

自己写的作业,简单易懂

Topic source

我有疫苗

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

把所有的名字变成首字母大写,其他的小写

def my_capitalize(str): s = ""

# 首字母的10进制值
f_hec = ord(str[0])
# 首字母大写
if f_hec > 97:
    f_hec = f_hec-32
s = s + chr(f_hec)
# 从第二个开始每个字符变成小写
for i in str[1:]:
    s_hec = ord(i)
    if s_hec < 97:
        s_hec = s_hec+32
    s = s+chr(s_hec)
return s

求积

def prod(L): def c(x,y): return x*y return reduce(c, L)

str2float

def str2float(str):

# 先找到小数点的位置
p = 0
for i in str:
    if i == '.':
        break
    p = p+1
# 小数点前面
a = str[:p]
# 小数点后面
b = str[p+1:]
l = len(b)
# str2int
a = int(a)
b = int(b)
return a + b / pow(10, l)

''' 注意: 1.str是不可改变对象 2.幂次方运算用pow函数 3.python中字符和整数不能直接比大小 '''


  • 1

Reply