for i in num[0:-1]: if i == '-': print(f"fu", end=' ') else: print(f"{pinyin_list[int(i)]}", end=' ')
print(pinyin_list[int(num[-1])])
# 2. 字符串拼接(会有额外的内存开销) pinyin_list = ['ling', 'yi', 'er', 'san', 'si', 'wu', 'liu', 'qi', 'ba', 'jiu'] num = input() s = '' for i in num: if i == '-': # print("fu",end=' ') s += "fu"+" " else: s += f"{pinyin_list[int(i)]}"+" " print(s.strip())
# 3. 字典(个人比较推荐字典的这种方法,) pinyin_dic = {'0': 'ling', '1': 'yi', '2': 'er', '3': 'san', '4': 'si', '5': 'wu', '6': 'liu', '7': 'qi', '8': 'ba', '9': 'jiu'} num = input() for n in num[0:-1]: if n == '-': print('fu', end=' ') else: print(pinyin_dic[n], end=' ') print(pinyin_dic[num[-1]])
l1-008 求整数段和
给定两个整数A和B,输出从A到B的所有整数以及这些数的和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
A, B = map(int, input().split()) # 总和 s = 0 # 计数器 n = 0 # 遍历A-B for i inrange(A, B+1): # >5d表示让每个字符右对齐并占五个字符宽度 print("{:>5d}".format(i), end='') s += i n += 1 # 一行打印五个数 if n % 5 == 0: print("\n", end='') n = 0 # 到最后一个数时换行打印Sum elif i == B: print("\n", end='') print(f"Sum = {s}")