Separating the digits of an integer

 For example, if I were to input 12345, the program should return 1, 2, 3, 4, and 5. 

There are two ways to do this, the 'cheat' way by changing the integer into a string and just taking the positions of the digits and fetching them that way. The other way is to divide it by powers of 10 and round it down to the nearest number, then minus that number from the original number, then take the resulting value, then minus all the number digits before that number:

import math
number = input("Please enter your number: ")


def stringway(input):
input = str(input)
digits_list = []
for x in range(0, len(input)):
digits_list.append(int(input[x]))
print(digits_list)


def integerway(input):
input = int(input)
digits_list = []
i = 1
while True:
if i == 1:
temp = (input / 10 ** i)
temp = math.floor(temp)
temp = input - temp * 10 ** i
digits_list.append(int(temp))
else:
temp = math.floor(input / 10 ** i)
temp = input - (temp * (10 ** i))
for x in range(0, len(digits_list)):
temp = temp - (digits_list[x] * (10 ** x))
temp = temp / (10 ** (i-1))
digits_list.append(int(temp))
if input / (10 ** i) < 1:
break
else:
i += 1
digits_list.reverse()
print(digits_list)


if __name__ == '__main__':
stringway(number)
integerway(number)

Comments