一、数字转人民币读法
把一个小于等于12位的浮点数转为中文读法。
def divide(num):
integer = int(num)
#round四舍五入函数
fraction = round((num - integer) * 100)
return (str(integer), str(fraction))
han_list = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
unit_list = ["十","百","千"]
def four_to_hanstr(num_str):
result = ""
num_len = len(num_str)
for i in range(num_len):
num = int(num_str[i])
if i != num_len-1 and num != 0:
result += han_list[num] + unit_list[num_len - 2 - i]
elif i>0 and num == 0:
if int(num_str[i-1]) == num:
result = result
else:
result += han_list[num]
else:
result += han_list[num]
return result
def integer_to_str(num_str):
str_len = len(num_str)
if str_len > 12:
print("数字太大,无法翻译")
return
elif str_len > 8:
return four_to_hanstr(num_str[:-8]) + "亿" +\
four_to_hanstr(num_str[-8:-4]) + "万" +\
four_to_hanstr(num_str[-4:]) + "元"
else:
return four_to_hanstr(num_str)
def fraction_to_str(num_frac):
return four_to_hanstr(num_frac[0]) + "角" + four_to_hanstr(num_frac[1]) + "分"
num = float(input("请输入一个浮点数: "))
#361092004.4567
integer, fraction = divide(num)
print(integer_to_str(integer),end="")
print(fraction_to_str(fraction))
输出结果:
二、控制台五子棋
利用二维列表还原五子棋小游戏