Reputation: 125
I an taking input from the user and the input will be more than 12 digits of number but i want the digits of the number individually so how can i increment the variable int like in the example below
for i in range(len(number)):
j = i + 1
dj = int(number[i])
In this i is the ith digit and j is the number that will be appended with the d variable.
will this work ? I want it like d1 = int(number[0])
and so on so that i can do math operation on them.
there can be 12 - 16 digits in the number the user will give as input.
i have tried pre-defining the variables but that gave a error when the d16 digit did not get a number because of the len of input was 15.
Upvotes: 0
Views: 772
Reputation: 1458
See if this helps:
number=str(input())
for i in range(len(number)):
j = i + 1
exec(f'd{j} = int(number[i])')
Upvotes: 1