Click here to Skip to main content
15,743,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I fix the task so that it can output [21, 2, 33], not [21,5,3]. I'm not sure what can i change

def to_number(digits):
    result=digits[0]
    for el in digits[1:]:
        result=result*10
        result=result+el
    return result
print(to_number([21, 2, 33]))


When i want to print single digits it works, but when they are two-digit number has some problems

What I have tried:

It's definitely something from here result=result+el because it collects the next element
Posted
Updated 9-Jul-22 0:43am

1 solution

That code correctly calculates the answer:
- result = 22
- result = 22 x 10 = 220
- result = 220 + 2 = 222
- result = 222 x 10 = 2220
- result = 2220 + 33 = 2253

It would probably work if all your digit values were actually less than 10.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900