python怎么取各个数位的数?
a = 12345 取个位 : b = (a / 1) % 10 = a % 10 取十位: b = (a / 10) % 10 取百位: b = (a / 100) % 10 以此类推。假设输入的数是n, n不为0 n=某数 while n>0。 (n,r) = divmod(n,10) print r 其中(n,r) = divmod(n,10) r是个位数。n是其它高位数,divmod包含 除和求余数.。或者用 [int(i) for i in str(n)] str(n) 把数变成字符串 int(i) 把字符串里的字母变成数字。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有