How to add numbers and letters in a string in Python3.3.5 (The end result should be int)?

My purpose:

"Write a function sumOfDigits, which has one parameter of the string type. The function should return the sum of the digits in the string. Do not treat multiple digital strings as one number -" 2014 "should be treated as 4 different digits from 2, 0, 1, 4. The function should return 17 for the string "Current Date 09/01/2014." You can assume that the parameter is a string. There is no need to perform type checking. "

Here is what I still have (with corresponding indentation):

def sumOfDigits (string1: str): summation=0 for i in string1: summation=summation + int (i) return (summation) print (sumOfDigits ('543tf')) 

I get the following error:

 "Traceback (most recent call last): File "C:\Users\Andrew\Desktop\lab3.py", line 45, in <module> print (sumOfDigits ('543tf')) File "C:\Users\Andrew\Desktop\lab3.py", line 42, in sumOfDigits summation=summation + int (i) ValueError: invalid literal for int() with base 10: 't'" 

How can i solve this? Does it because of the difficulties associated with adding int and line / char?

+5
source share
5 answers

The problem is that int(x) fails if x not a digit. The solution is to sum only the numbers. For this purpose, python has an isdigit string method. For instance:

 >>> s = '543tf' >>> sum(int(x) for x in s if x.isdigit()) 12 

and

 >>> s = "Today's date is 09/01/2014" >>> sum(int(x) for x in s if x.isdigit()) 17 

Python3

Unicode adds a large number of characters, and python classifies some of them as numbers, even if int does not accept them. One such example is superscript 3: ³ . To handle this, python3 introduced isdecimal and made it more discriminatory than isdigit . So in python3 you can use:

 >>> s = "Today's date is 09/01/2014 See footnote³" >>> sum(int(x) for x in s if x.isdecimal()) 17 
+3
source

You can perform type checking, as other people have mentioned, or just pass exception as follows (with minimal code modification, so it's easy to see how this works):

 def sumOfDigits (string1): summation=0 for i in string1: try: summation = summation + int(i) except: pass return (summation) 

This is one of the philosophies of Python that says you think the input is correct and only catch exceptions and handle them separately. You might even want to explicitly check for a ValueError , rather than finding all exceptions. The bit of detail is up to you.

+3
source

You cannot cast char to int and expect it to return 0 unless otherwise it will be int and its numeric value. You need to split the string, check if the characters represent integers, and add them up. Like this:

 def sumdigits(mystring): summation = 0 for letter in list(mystring): if letter.isdigit(): summation += int(letter) return summation 
+1
source

This will do the trick:

 sum(int(d) for d in '09/01/2014' if d.isdigit()) 

but I do not recommend passing it on without explaining how it works :-)

You can also try to figure out how

 sum(map(int,filter(str.isdigit,'09/01/2014'))) 

work.

Your attempt does not fail: "due to the difficulty of adding ints to char" (at least not directly), it actually does not work before that: when you try to convert something that does not look remotely like a number ('t') in int . This is what the error message is trying to tell you (right at the bottom: "ValueError, etc."). How do you solve it? You will remove material that cannot be converted to int before you try to convert. The two code samples I gave you contain tips that suggest two different ways you could do this.

+1
source

There are many ways to discard this cat.

One of them is to use a regular expression to search for single digits:

 >>> import re >>> re.findall(r'(\d)', '543tf') ['5', '4', '3'] 

Put this inside map and convert to ints:

 >>> map(int, re.findall(r'(\d)', '543tf')) [5, 4, 3] 

Put this in sum to sum the list:

 >>> sum(map(int, re.findall(r'(\d)', '543tf'))) 12 
+1
source

Source: https://habr.com/ru/post/1204460/


All Articles