create a 20 digit string, and cast to a list then add all the digits as integers - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: create a 20 digit string, and cast to a list then add all the digits as integers (/thread-4028.html) |
create a 20 digit string, and cast to a list then add all the digits as integers - nikhilkumar - Jul-18-2017 my question here create a 20 digit string, and cast to a list then add all the digits as integers print the equation and answer Hint: use cast to sum the digits, and .join() to create the equation (1+2+3+...) new_digit="20,10,70,30,20" print(list(new_digit)) sum_of_digits=0 new_digit=int(new_digit) for digits in new_digit: #digits=int(digits) sum_of_digits+=digits print("+".join(new_digit))But I am getting ValueError: invalid literal for int() with base 10: '20,10,70,30,20' RE: create a 20 digit string, and cast to a list then add all the digits as integers - wavic - Jul-18-2017 I presume that this is a home work. You can't just turn a string with commas to integers: >>> new_digit="20,10,70,30,20" >>> numbers = list() >>> for number in new_digit.split(','): ... numbers.append(int(number)) >>> numbers [20, 10, 70, 30, 20] RE: create a 20 digit string, and cast to a list then add all the digits as integers - nilamo - Jul-19-2017 Quote:new_digit=int(new_digit)Try this instead: new_digit = map(int, new_digit.split(",")) |