I have to write a code where if I put a 3 digit number in, it will count how many hundreds, tens and ones are in it e.g 327=3 hunders, 2 tens. 7. ones
Question:
num = int(input('Give me a 3 digit number! :'))
div1 = 100
div2 = 110
div3 = 36
hundreds = num // div1
tens = num // div2
ones = num // div3
print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))
Output (It should be; "In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found")
The real results
Give me a 3 digit number! : 187
In 187 there are 1 hundred(s) 1 ten(s) and 5 ones are found
Answers:
Use the modulus operator:
num = int(input('Give me a 3 digit number! :'))
hundreds = num // 100
tens = num % 100 // 10
ones = num % 10
print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))
Result:
Give me a 3 digit number! :187
In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found
you could simply work with the string, there is no need of calculations, only index the string by order
num = input('Give me a 3 digit number! :')
assert num.isnumeric() and len(num) == 3 # this line verify that the input is a integer and has 3 digits
print(f"In {num} there are {num[0]} hundred(s) {num[1]} ten(s) and {num[2]} ones are found")
Give me a 3 digit number! :456
In 456 there are 4 hundred(s) 5 ten(s) and 6 ones are found
I found myself bored while waiting for a delivery. I passed the time building a general solution to this problem:
labels = [(10, 'one'), (10, 'ten'),
(10, 'hundred'), (1000, 'thousand'),
(1000, 'million'), (1000, 'billion')]
def write_it_out(num):
p = 0
parts = []
while num > 0 and p < len(labels):
num, m = divmod(num, labels[p][0])
parts.append((m, labels[p][1]))
p += 1
r = ""
for part in parts[-1::-1]:
if not part[0]:
continue
if r:
r += ", "
r += f"{part[0]} {part[1]}{'s' if part[0] > 1 else ''}"
return r
# num = int(input('Give me a 3 digit number! :'))
# print(write_it_out(num))
def test(num):
print(f"{num}: {write_it_out(num)}")
test(187)
test(1)
test(2)
test(440)
test(19)
test(121)
test(121003)
test(1234)
test(123456789)
test(12345678900)
test(1000000000)
Result:
187: 1 hundred, 8 tens, 7 ones
1: 1 one
2: 2 ones
440: 4 hundreds, 4 tens
19: 1 ten, 9 ones
121: 1 hundred, 2 tens, 1 one
121003: 121 thousands, 3 ones
1234: 1 thousand, 2 hundreds, 3 tens, 4 ones
123456789: 123 millions, 456 thousands, 7 hundreds, 8 tens, 9 ones
12345678900: 12 billions, 345 millions, 678 thousands, 9 hundreds
1000000000: 1 billion
It will mess up if you give it a negative number or a number of 1000 billion or greater. Fixing that by adding more labels is left to the reader.
PS: Thanks @Sterling for pointing me to divmod
!
num = int(input('Give me a 3 digit number! :'))
div1 = 100
div2 = 110
div3 = 36
hundreds = num // div1
tens = num // div2
ones = num // div3
print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))
Output (It should be; "In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found")
The real results
Give me a 3 digit number! : 187
In 187 there are 1 hundred(s) 1 ten(s) and 5 ones are found
Use the modulus operator:
num = int(input('Give me a 3 digit number! :'))
hundreds = num // 100
tens = num % 100 // 10
ones = num % 10
print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))
Result:
Give me a 3 digit number! :187
In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found
you could simply work with the string, there is no need of calculations, only index the string by order
num = input('Give me a 3 digit number! :')
assert num.isnumeric() and len(num) == 3 # this line verify that the input is a integer and has 3 digits
print(f"In {num} there are {num[0]} hundred(s) {num[1]} ten(s) and {num[2]} ones are found")
Give me a 3 digit number! :456
In 456 there are 4 hundred(s) 5 ten(s) and 6 ones are found
I found myself bored while waiting for a delivery. I passed the time building a general solution to this problem:
labels = [(10, 'one'), (10, 'ten'),
(10, 'hundred'), (1000, 'thousand'),
(1000, 'million'), (1000, 'billion')]
def write_it_out(num):
p = 0
parts = []
while num > 0 and p < len(labels):
num, m = divmod(num, labels[p][0])
parts.append((m, labels[p][1]))
p += 1
r = ""
for part in parts[-1::-1]:
if not part[0]:
continue
if r:
r += ", "
r += f"{part[0]} {part[1]}{'s' if part[0] > 1 else ''}"
return r
# num = int(input('Give me a 3 digit number! :'))
# print(write_it_out(num))
def test(num):
print(f"{num}: {write_it_out(num)}")
test(187)
test(1)
test(2)
test(440)
test(19)
test(121)
test(121003)
test(1234)
test(123456789)
test(12345678900)
test(1000000000)
Result:
187: 1 hundred, 8 tens, 7 ones
1: 1 one
2: 2 ones
440: 4 hundreds, 4 tens
19: 1 ten, 9 ones
121: 1 hundred, 2 tens, 1 one
121003: 121 thousands, 3 ones
1234: 1 thousand, 2 hundreds, 3 tens, 4 ones
123456789: 123 millions, 456 thousands, 7 hundreds, 8 tens, 9 ones
12345678900: 12 billions, 345 millions, 678 thousands, 9 hundreds
1000000000: 1 billion
It will mess up if you give it a negative number or a number of 1000 billion or greater. Fixing that by adding more labels is left to the reader.
PS: Thanks @Sterling for pointing me to divmod
!