python练习题–阿拉伯数字转换成中文数字

用户输入任意阿拉伯数字,如【123456】,把它转换成中文数字【壹拾贰万叁仟肆佰伍拾陆】

以前面试时面试官问的问题,当时没能答出来,今天初步解决,把它记录下来,也是有点意义的。本题并不完善,不包括0(包括0的格式问题尚未解决),纯属提供一个思路(获取位数的思路)


```python
def switch_num(num):"""把数字转换成中文"""if type(num) != int:return Noneelif num == 1:return "壹"elif num == 2:return "贰"elif num == 3:return "叁"elif num == 4:return "肆"elif num == 5:return "伍"elif num == 6:return "陆"elif num == 7:return "柒"elif num == 8:return "捌"elif num == 9:return "玖"# 判断位数并转换,//运算符表示除法运算后向下取整, %运算符表示模运算,即除法运算后取余数
while True:try:n = int(input("输入大于或等于0且位数不大于9位的数字:"))if n > 0:if len(str(n)) == 9:a = n // 100000000a = switch_num(a) + "亿"b = n // 10000000 % 10b = switch_num(b) + "千"c = n // 1000000 % 10c = switch_num(c) + "佰"d = n // 100000 % 10d = switch_num(d) + "拾"e = n // 10000 % 10e = switch_num(e) + "万"f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(a + b + c + d + e + f + g + h + i)elif len(str(n)) == 8:b = n // 10000000 % 10b = switch_num(b) + "千"c = n // 1000000 % 10c = switch_num(c) + "佰"d = n // 100000 % 10d = switch_num(d) + "拾"e = n // 10000 % 10e = switch_num(e) + "万"f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(b + c + d + e + f + g + h + i)elif len(str(n)) == 7:c = n // 1000000 % 10c = switch_num(c) + "佰"d = n // 100000 % 10d = switch_num(d) + "拾"e = n // 10000 % 10e = switch_num(e) + "万"f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(c + d + e + f + g + h + i)elif len(str(n)) == 6:d = n // 100000 % 10d = switch_num(d) + "拾"e = n // 10000 % 10e = switch_num(e) + "万"f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(d + e + f + g + h + i)elif len(str(n)) == 5:e = n // 10000 % 10e = switch_num(e) + "万"f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(e + f + g + h + i)elif len(str(n)) == 4:f = n // 1000 % 10f = switch_num(f) + "仟"g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(f + g + h + i)elif len(str(n)) == 3:g = n // 100 % 10g = switch_num(g) + "佰"h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(g + h + i)elif len(str(n)) == 2:h = n // 10 % 10h = switch_num(h) + "十"i = n // 1 % 10i = switch_num(i)print(h + i)elif len(str(n)) == 1:i = n // 1 % 10i = switch_num(i)print(i)else:print("请输入不超过9位的数字!")else:print("输入错误,请输入大于0的数字!")except:print("输入错误,请输入数字!")

python练习题--阿拉伯数字转换成中文数字相关推荐

  1. python将数字转变为中文读法-python中将阿拉伯数字转换成中文的实现代码

    代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass ...

  2. 【工具封装】Python 实现将阿拉伯数字 === 转换成中文大写数字

    一.序言:   工具封装第四弹,阿拉伯数字 ===> 转换成 ===> 中文大写数字,喜欢就赶紧收藏+点赞+关注吧 !!! ---- Nick.Peng 二.实现代码如下: #!/usr/ ...

  3. 将数字转换成中文数字

    将阿拉伯数字转换成中文数字,大家可以看我的测试数据,应该没有BUG. /** * 将数字转换成中文数字 * @author Prosper * */ public class IntToCN { pu ...

  4. js将阿拉伯数字转换成中文的大写数字

    js将阿拉伯数字转换成中文的大写数字 export const numberToChinese = (num) => {var AA = new Array("零", &qu ...

  5. 将一组阿拉伯数字转换成中文大写数字

    题目大概:  将一组阿拉伯数字转换成中文大写数字  52306 ==> 伍万贰千叁百零陆 我实现了将文件中的一组数字(每行为一个数)  形如: Java代码   25364 466932300 ...

  6. 《读九章算术学Python》如何用Python编程实现阿拉伯数字转换成汉字数字?

    第6章 数量转换 Python编程基础 字典 字符串操作 if-elif-else语句 递归 前面的输入和输出都是阿拉伯数字,这一章我们来看一下如何实现阿拉伯数字和汉字数字之间的相互转换. 6.1 阿 ...

  7. 阿拉伯数字转换成中文算法--计数单位

    今天继续看<算法的乐趣>,学习了阿拉伯数字与中文数字的转化. 汉字用零一二三四五六七八九作为基本计数,与阿拉伯数字靠数字偏移位置的权位不一样,中文数字是才有"数字+权位" ...

  8. java练习:金额转换,阿拉伯数字转换成中文传统形式

    需求:金额转换,阿拉伯数字转换成中文传统形式   ,例如 101000001010   转为     壹仟零壹拾亿零壹仟零壹拾圆整 最终版: import java.util.Scanner; pub ...

  9. win7计算机名改成大写,处置win7系统将word中的阿拉伯数字转换成大写数字的还原方案...

    随着电脑的使用率越来越高,我们有时候可能会遇到对win7系统将word中的阿拉伯数字转换成大写数字进行设置,如果我们需要对win7系统将word中的阿拉伯数字转换成大写数字进行设置时,要怎么处理win ...

最新文章

  1. 微信小程序-锚点定位+内容滑动控制导航选中
  2. C# WinForm TreeView用法总结
  3. 如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
  4. php表单验证并使值变化,php – Zend_Form手动设置和验证字段值
  5. pat 乙级 1056 组合数的和(C++)
  6. python eval 用法
  7. yuv420(planer) to bgr24 to bmp
  8. Flutter实战一Flutter聊天应用(五)
  9. java frameview_Java FrameLayout.removeView方法代码示例
  10. Mac 使用Navicat连接Oracle提示:ORA-21561: OID generation failed
  11. android瀑布流插件,jQuery瀑布流插件 Masonry
  12. python笔记 同行输出 print格式化输出
  13. Html5常见面试题总结
  14. 终于明白带宽和频率的关系
  15. Cannot find module coa/compile.js
  16. [九度][何海涛] 斐波那契数列
  17. DAC7512驱动原理
  18. 地级市GDP地级市一二三产业GDP面板数据(1999-2021年)
  19. c语言——制作钟表(与系统时间同步)
  20. 五轴数控转台_青岛国际机床展重要看点之一:创新的五轴加工技术

热门文章

  1. 小微企业如何通过运营公众号提升业绩?
  2. Hello Riak
  3. 如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
  4. 大华网络摄像头通过gstreamer 获取不到RTSP流
  5. 用户盘云存储——百度网盘
  6. 怎么设置指定号码打不进来_如何屏蔽某个电话号码,让它永远打不进来??
  7. java证书(java证书过期怎么办)
  8. 对于学习率与梯度下降的通俗总结:
  9. Java中方法调用参数传递的方式是传值,尽管传的是引用的值而不是对象的值。(Does Java pass by reference or pass by value?)
  10. 算法 2.二进制加法