Python Forum

Full Version: Changing units with tens
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
i have to draw int number from (1000, 9999) and create new number where I have to change units with tens and hundreds with thousands from the first number, Im new in python so it has to be done in the simplest way. I have something like this:

import random
from typing import Final
RANGE_MIN: Final[int] = 1000
RANGE_MAX: Final[int] = 9999
n = random.randint(RANGE_MIN, RANGE_MAX)
print(f'{n = }')
units = int((n // 10 ** 0) % 10)
tens = (n // 10 ** 1) % 10
hundreds = (n // 10 ** 2) % 10
thousands = (n // 10 ** 3) % 10
print(f'{units = }\n{tens = }\n{hundreds = }\n{thousands = }')
Now i dont know how to create the new number
From what I can see (but I may be wrong about this) I think the only issue you have, is with your print() function formatting.

# line 6
print(f'n = {n}')

# line 11
print(f'units = {units}\ntens = {tens}\nhundreds = {hundreds}\nthousands = {thousands}')
Output:
#test input n = 1425 #output n = 1425 units = 5 tens = 2 hundreds = 4 thousands = 1