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)
I tried this but it does not work and I'm stuck.
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)
I tried this but it does not work and I'm stuck.
You might want to try something like following:
def get_pos_nums(num):
pos_nums = []
while num != 0:
pos_nums.append(num % 10)
num = num // 10
return pos_nums
And call this method as following.
>>> get_pos_nums(9876)
[6, 7, 8, 9]
The 0th
index will contain the units, 1st
index will contain tens, 2nd
index will contain hundreds and so on...
This function will fail with negative numbers. I leave the handling of negative numbers for you to figure out as an exercise.
Like this?
a = str(input('Please give me a number: '))
for i in a[::-1]:
print(i)
Demo:
Please give me a number: 1324
4
2
3
1
So the first number is ones, next is tens, etc.
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
"//" in Python stands for integer division. It largely removes the fractional part from the floating-point number and returns an integer
For example:
4/3 = 1.333333
4//3 = 1
You could try splitting the number using this function:
def get_place_values(n):
return [int(value) * 10**place for place, value in enumerate(str(n)[::-1])]
For example:
get_place_values(342)
>>> [2, 40, 300]
Next, you could write a helper function:
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'))]}"
Then you can combine the two like so:
def print_place_values(n):
for value in get_place_values(n):
print(get_place_val_to_word(value))
For example:
num = int(input("Please give me a number: "))
# User enters 342
print_place_values(num)
>>> 2 ones
4 tens
3 hundreds
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)
this does the job. it is simple to understand as well.
Please note that I took inspiration from the above answer by 6pack kid to get this code. All I added was a way to get the exact place value instead of just getting the digits segregated.
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)
Once you run this code, for the input of 12345 this is what will be the output:
Enter Number: 12345
[5, 40, 300, 2000, 10000]
This helped me in getting an answer to what I needed.
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)
Quickest way:
num = str(input("Please give me a number: "))
print([int(i) for i in num[::-1]])
This will do it, doesn't use strings at all and handles any integer passed for col
sensibly.
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)
usage:
print(tenscol(9785,-1))
print(tenscol(9785,1))
print(tenscol(9785,2))
print(tenscol(9785,3))
print(tenscol(9785,4))
print(tenscol(9785,99))
Output:
5
5
8
7
9
9
def get_pos(num,unit):
return int(abs(num)/unit)%10
So for "ones" unit is 1 while for "tens", unit is 10 and so forth.
It can handle any digit and even negative numbers effectively.
So given the number 256, to get the digit in the tens position you do
get_pos(256,10)
>> 5
I had to do this on many values of an array, and it's not always in base 10 (normal counting - your tens, hundreds, thousands, etc.). So the reference is slightly different: 1=1st place (1s), 2=2nd place (10s), 3=3rd place (100s), 4=4th place (1000s). So your vectorized solution:
import numpy as np
def get_place(array, place):
return (array/10**(place-1)%10).astype(int)
Works fast and also works on arrays in different bases.
# 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)
Divide by place value for you:)
It gets an error in one of my programs.
pos_nums = []
def get_pos_nums(num):
while num != 0:
pos_nums.append(num % 10)
num = num // 10
return pos_nums
def return_divisible(number_to_divide_by_place):
y = 0
string = ""
divisible = "0"
get_pos_nums(9999)
for x in pos_nums:
y += 1
if y > 1:
string += "0"
divisible = "1" + string
divisible = int(divisible)
divideddivisible = number_to_divide_by_place/divisible
print(divideddivisible)
return(divideddivisible)
return_divisible(9999)
In Python, you can try this method to print any position of a number.
For example, if you want to print the 10 the position of a number, Multiply the number position by 10, it will be 100, Take modulo of the input by 100 and then divide it by 10.
Note: If the position get increased then the number of zeros in modulo and in divide also increases:
input = 1234
print(int(input % 100) / 10 )
Output:
3
python def tens_of_number(number: int): return number % 100 / 10 num = 1234 print("tens of ", num, ":", tens_of_number(num))
–
Prophesy So I saw what another users answer was and I tried it out and it didn't quite work, Here's what I did to fix the problem. By the way I used this to find the tenth place of a number
# Getting an input from the user
input = int(input())
# Finding the tenth place of the number
print(int(input % 100) // 10)
© 2022 - 2024 — McMap. All rights reserved.
120 // 10
is12
. – Hedron