Question

Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum...

Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum of the digits multiplied by two. A solution using a loop is expected.

As an example, the following code fragment:

total = double_add_digits_in_string("xx1xx2xx3xx") print (total)

should produce the output:

12

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def double_add_digits_in_string(st):
    # Initialize total to zero
    total = 0
    # Looping through each character
    for x in st:
        # checking x is a digit
        if(x>='0' and x<='9'):
            # adding int value of x to total
            total += int(x)
    # Returning double of total
    return 2*total

# Testing
total = double_add_digits_in_string("xx1xx2xx3xx")
print (total)

Output:

12

Add a comment
Know the answer?
Add Answer to:
Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT