![](/img/trans.png)
[英]How to find tens/ones digit of two different numbers regardless of their length
[英]How to find the numbers in the thousands, hundreds, tens, and ones place in PYTHON for an input number? For example: 256 has 6 ones, 5 tens, etc
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)
我试过了,但它不起作用,我被卡住了。
您可能想尝试以下操作:
def get_pos_nums(num):
pos_nums = []
while num != 0:
pos_nums.append(num % 10)
num = num // 10
return pos_nums
并按如下方式调用此方法。
>>> get_pos_nums(9876)
[6, 7, 8, 9]
第0th
索引将包含单位, 1st
索引将包含十个, 2nd
索引将包含数百个等等......
此函数将因负数而失败。 我将负数的处理留给您作为练习。
像这样?
a = str(input('Please give me a number: '))
for i in a[::-1]:
print(i)
演示:
Please give me a number: 1324
4
2
3
1
所以第一个数字是一个,接下来是十个,依此类推。
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
Python 中的“//”代表整数除法。 它在很大程度上从浮点数中删除小数部分并返回一个整数
例如:
4/3 = 1.333333
4//3 = 1
请注意,我从 6pack Kid 的上述答案中获得灵感来获取此代码。 我添加的只是一种获取确切位置值的方法,而不仅仅是将数字隔离。
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)
运行此代码后,对于 12345 的输入,这将是输出:
Enter Number: 12345
[5, 40, 300, 2000, 10000]
这帮助我得到了我需要的答案。
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)
您可以尝试使用此函数拆分数字:
def get_place_values(n):
return [int(value) * 10**place for place, value in enumerate(str(n)[::-1])]
例如:
get_place_values(342)
>>> [2, 40, 300]
接下来,您可以编写一个辅助函数:
def get_place_val_to_word(n):
n_str = str(n)
num_to_word = {
"0": "ones",
"1": "tens",
"2": "hundreds",
"3": "thousands"
}
return f"{n_str[0]} {num_to_word[str(n_str.count('0'))]}"
然后你可以像这样将两者结合起来:
def print_place_values(n):
for value in get_place_values(n):
print(get_place_val_to_word(value))
例如:
num = int(input("Please give me a number: "))
# User enters 342
print_place_values(num)
>>> 2 ones
4 tens
3 hundreds
最快的方法:
num = str(input("Please give me a number: "))
print([int(i) for i in num[::-1]])
这将做到这一点,根本不使用字符串并明智地处理传递给col
任何整数。
def tenscol(num: int, col: int):
ndigits = 1
while (num % (10**ndigits)) != num:
ndigits += 1
x = min(max(1, col), ndigits)
y = 10**max(0, x - 1)
return int(((num % 10**x) - (num % y)) / y)
用法:
print(tenscol(9785,-1))
print(tenscol(9785,1))
print(tenscol(9785,2))
print(tenscol(9785,3))
print(tenscol(9785,4))
print(tenscol(9785,99))
输出:
5
5
8
7
9
9
def get_pos(num,unit):
return (num//unit)%10
因此,“个”的单位是 1,而“十”的单位是 10,依此类推。
它可以有效地处理任何数字甚至负数。
所以给定数字 256,要得到十位位置的数字,你所做的
get_pos(256,10)
>> 5
num=1234
digit_at_one_place=num%10
print(digit_at_one_place)
digits_at_tens_place=(num//10)%10
digits_at_hund_place=(num//100)%10
digits_at_thou_place=(num//1000)%10
print(digits_at_tens_place)
print(digits_at_hund_place)
print(digits_at_thou_place)
这可以完成工作。 也很容易理解。
我知道了。 这是因为input()
返回一个字符串,因此不能用于数学运算。 只需使用int(input())
代替。
我必须对数组的许多值执行此操作,并且它并不总是以 10 为底(正常计数 - 您的十、百、千等)。 所以引用略有不同:1=第1位(1s),2=第2位(10s),3=第3位(100s),4=第4位(1000s)。 所以你的矢量化解决方案:
import numpy as np
def get_place(array, place):
return (array/10**(place-1)%10).astype(int)
工作速度很快,也可以在不同基地的 arrays 上工作。
# method 1
num = 1234
while num>0:
print(num%10)
num//=10
# method 2
num = 1234
print('Ones Place',num%10)
print('tens place',(num//10)%10)
print("hundred's place",(num//100)%10)
print("Thousand's place ",(num//1000)%10)
在 Python 中,您可以尝试使用此方法打印数字的任何位置。
例如,如果要打印数字的位置 10,将数字位置乘以 10,将是 100,将输入的模除以 100,然后将其除以 10。
注意:如果位置增加,则模数和除法中的零数也会增加:
input = 1234
print(int(input % 100) / 10 )
输出:
3
所以我看到了另一个用户的回答是什么,我试了一下,但它并没有完全奏效,这是我为解决这个问题所做的。 顺便说一句,我用它来找到一个数字的第十位
# Getting an input from the user
input = int(input())
# Finding the tenth place of the number
print(int(input % 100) // 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.