Python Forum
create a 20 digit string, and cast to a list then add all the digits as integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a 20 digit string, and cast to a list then add all the digits as integers
#1
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'
Reply
#2
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]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Quote:new_digit=int(new_digit)
Try this instead:
new_digit = map(int, new_digit.split(","))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
Question How can I create a linked list that contains each folder with its files? noahverner1995 8 2,356 Dec-25-2021, 10:46 PM
Last Post: noahverner1995
  TypeError: string indices must be integers, not str hanieh 4 98,313 Jan-04-2021, 05:13 AM
Last Post: delonbest
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,787 May-28-2020, 09:50 AM
Last Post: bryan0901
  Sorting digits in a mixed string snorri 1 1,625 Apr-22-2020, 11:04 AM
Last Post: buran
  Convert a list of integers to strings? Cornelis 3 2,220 Nov-15-2019, 12:13 PM
Last Post: perfringo
  TypeError: string indices must be integers thecoziest 3 6,183 Sep-29-2019, 05:34 PM
Last Post: ichabod801
  Four-digit number text translation. soz 3 2,650 May-13-2019, 03:02 PM
Last Post: buran
  create three digit numbers Krszt 4 4,397 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,331 Apr-20-2018, 02:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020