|
发表于 2022-3-7 00:01:16
|
显示全部楼层
数字转英文:
- # 将 0 ~ 9 的数字转换成英文
- def unit_to_word(u):
- words = {
- 0: 'zero', 1: 'one', 2: 'two', 3: 'three',
- 4: 'four', 5: 'five', 6: 'six', 7: 'seven',
- 8: 'eight', 9: 'nine'
- }
- return words[u]
- # 将 10 ~ 19,20 ~ 99 的十位部分数字转换成英文
- def tens_to_word(t):
- words = {
- 10: 'ten', 11: 'eleven', 12: 'twelve',
- 13: 'thirteen', 14: 'fourteen', 15: 'fifteen',
- 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
- 19: 'nineteen', 20: 'twenty', 30: 'thirty',
- 40: 'forty', 50: 'fifty', 60: 'sixty',
- 70: 'seventy', 80: 'eighty', 90: 'ninety'
- }
- if 10 <= t < 20 or not t % 10:
- return words[t]
- else:
- a, b = divmod(t, 10)
- return words[a*10] + ' ' + unit_to_word(b)
- # 将 百位数字转换成英文
- def hundreds_to_word(h):
- a, b = divmod(h, 100)
- if b < 10:
- return f"{unit_to_word(a)} hundred and {unit_to_word(b)}"
- else:
- return f"{unit_to_word(a)} hundred and {tens_to_word(b)}"
- print(hundreds_to_word(729))
复制代码
输出日历:
- def day(y, m, d):
- ans = 0
- month = [31,29,31,30,31,30,31,31,30,31,30,31]
-
- if not isLeapYear(y):
- month[1] = 28
-
- for i in range(1,y): # 注意计算之前年的天数时要从1开始计算
- if isLeapYear(i):
- ans += 366
- # ans = ans % 7
- else:
- ans += 365
- # ans = ans % 7
-
- for i in range(m-1): # 这里因为月份是由数组表示的,从1月加到m-1月在数组中就是从下标0加到下标m-2
- ans += month[i]
- # ans = ans % 7
-
- ans += d
-
- return ans % 7# 最后这里对7取余也可以,如果计算的年数过多可以边加天数,边对7取余,就像注释掉的那些代码一样
- def isLeapYear(y):
- if (y % 400 == 0) or (y % 100 != 0 and y % 4 == 0):
- return True
- else:
- return False
- def calendar(y, m):
- month = [31,29,31,30,31,30,31,31,30,31,30,31]
-
- if not isLeapYear(y):
- month[1] = 28
- print(f"\t\t\t{y}年{m}月\t\t\t")
- print()
- print('\t'.join(['Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa']))
- temp = [''] * 7
- d = month[m-1]
- for i in range(1, d + 1):
- temp[day(y, m, i)] = str(i)
- if temp[-1] != '' or i == d:
- print('\t'.join(temp))
- temp = [''] * 7
- calendar(2022, 3)
复制代码 |
|