OmKshirsagar
OmKshirsagar

Reputation: 125

how can i just increment a variable so that it store the value i want it to?

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

Answers (1)

Raghul Raj
Raghul Raj

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

Related Questions