How do I split the numbers or values I enter into a list?

Asked 2 years ago, Updated 2 years ago, 25 views

I want to take 5 values and add 1+2+3+4+5 = 15 <- I want to make this value [1,5] as a list Is it possible to enter only the character value by using the list function...? Or is it because there is only one value? Anyway, you can change it to str and then change it to map function, but there is an error when you use it in the repetitive sentence. You have to keep adding the value separated by the list...Is there a way to divide this all at once?

arr = []
result = 1
num = int (input ("repeated numeric")
for i in range(num):
    num2 = int (input ("numeric input")
    arr.append(num2)
    print(arr)

for j in arr:
    result += j
    result = list(str(result))
    result = list(map(int, result))
print(result)

python

2022-09-22 19:39

2 Answers

The question itself is not fully understood. I don't understand that it should be done especially in repetitive sentences.

>>> list(map(int, list(str(15))))
[1, 5]


2022-09-22 19:39

I'm not sure about the rules.

If you add up from 1 to 15, it's 120 If it's 1, 20, it's 3.

def sum_of_item(number):
    L = list(map(int, list(str(number))))
    print(L)
    sum_L = sum(L)
    print(sum_L)
    if sum_L >= 10:
        sum_of_item(sum_L)

sum_L = sum(range(1, 11))
print(sum_L)
sum_of_item(sum_L)

55
[5, 5]
10
[1, 0]
1

If the range is 1 and 16,
120
[1, 2, 0]
3


2022-09-22 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.