Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PYTHON中找到输入数字的千、百、十和一中的数字?例如:256有6个一、5个十等_Python_Numbers_Operators - Fatal编程技术网

如何在PYTHON中找到输入数字的千、百、十和一中的数字?例如:256有6个一、5个十等

如何在PYTHON中找到输入数字的千、百、十和一中的数字?例如:256有6个一、5个十等,python,numbers,operators,Python,Numbers,Operators,我试过了,但没用,我卡住了。像这样吗 num = int(input("Please give me a number: ")) print(num) thou = int((num // 1000)) print(thou) hun = int((num // 100)) print(hun) ten =int((num // 10)) print(ten) one = int((num // 1)) print(one) 演示: a = str(input('Please give me

我试过了,但没用,我卡住了。

像这样吗

num = int(input("Please give me a number: "))
print(num)
thou = int((num // 1000))
print(thou)
hun = int((num // 100))
print(hun)
ten =int((num // 10))
print(ten)
one = int((num // 1))
print(one)

演示:

a = str(input('Please give me a number: '))

for i in a[::-1]:
    print(i)

因此,第一个数字是1,第二个数字是10,等等。

您可能需要尝试以下方法:

Please give me a number: 1324
4
2
3
1
并按如下方式调用此方法

def get_pos_nums(num):
    pos_nums = []
    while num != 0:
        pos_nums.append(num % 10)
        num = num // 10
    return pos_nums
0th
索引将包含单位,
1st
索引将包含十个单位,
2nd
索引将包含数百个单位,以此类推


此功能将因负数而失败。我把负数的处理留给你作为一个练习。

请注意,我从6pack kid的上述答案中获得了灵感,从而获得了这个代码。我所添加的只是一种获得精确位置值的方法,而不是仅仅将数字分隔开

>>> get_pos_nums(9876)
[6, 7, 8, 9]
运行此代码后,对于12345的输入,将输出以下内容:

num = int(input("Enter Number: "))
c = 1
pos_nums = []
while num != 0:
    z = num % 10
    pos_nums.append(z *c)
    num = num // 10
    c = c*10
print(pos_nums)

这帮助我得到了我所需要的答案。

在Python中,您可以尝试使用此方法打印数字的任何位置

Enter Number: 12345
[5, 40, 300, 2000, 10000]
例如,如果要打印数字位置的10, 将数字位置乘以10,等于100, 将输入的模除以100,然后除以10

注:如果位置增加,则模和除法中的零数量也会增加:

money = int(input("Enter amount: "))
thousand = int(money // 1000)
five_hundred = int(money % 1000 / 500)
two_hundred = int(money % 1000 % 500 / 200)
one_hundred = int(money % 1000 % 500 % 200 / 100)
fifty  = int(money % 1000 % 500 % 200 % 100 / 50)
twenty  = int(money % 1000 % 500 % 200 % 100 % 50 / 20)
ten  = int(money % 1000 % 500 % 200 % 100 % 50 % 20 / 10)
five  = int(money % 1000 % 500 % 200 % 100 % 50 % 20 % 10 / 5)
one = int(money % 1000 % 500 % 200 % 100 % 50 % 20 % 10 % 5 / 1)
if thousand >=1: 
  print ("P1000: " , thousand)
if five_hundred >= 1:
  print ("P500: " , five_hundred)
if two_hundred >= 1:
  print ("P200: " , two_hundred)
if one_hundred >= 1:
  print ("P100: " , one_hundred)
if fifty >= 1:
  print ("P50: " , fifty)
if twenty >= 1:
  print ("P20: " , twenty)
if ten >= 1:
  print ("P10: " , ten)
if five >= 1:
  print ("P5: " , five)
if one >= 1:
  print ("P1: " , one)
输出:

input = 1234

print(int(input % 100) / 10 )
Python中的“//”代表整数除法。它主要从浮点数中删除小数部分并返回整数

例如:

num = 1234

thousands = num // 1000
hundreds = (num % 1000) // 100
tens = (num % 100) // 10
units = (num % 10)

print(thousands, hundreds, tens, units)
# expected output: 1 2 3 4

大家好,欢迎来到Stack Overflow。你的问题能更具体一点吗?“它不工作”是什么意思?请包括实际输出和预期输出ᵩ 确保这不起作用,因为
120//10
12
。如果需要int到文本的转换,那么基于代码样式、基于函数的程序可能会更好:
python def tens\u of_number(number:int):返回编号%100/10 num=1234打印(“tens of”,num,“:”,tens\u of_number(num))
num = 1234

thousands = num // 1000
hundreds = (num % 1000) // 100
tens = (num % 100) // 10
units = (num % 10)

print(thousands, hundreds, tens, units)
# expected output: 1 2 3 4
4/3 = 1.333333
4//3 = 1