Question

Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer.

This function should add the digits of a string that are in an even position.

If there are no digits, the function should return -1.

As an example, the following code fragment:

string = "a12b056jk"; result=AddEvenPosDigs(string); print(result)

should produce the output:

8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for posting the question, here is the function that adds up the digits found in the even positions

__________________________________________________________________________________________________

def AddEvenPosDigs(string):
    sum=-1
    for i in range(len(string)):
        if i%2==0 and string[i].isdigit():
            sum+=int(string[i])
    if sum==-1:
        return sum
    else:
        return sum+1

string = "a12b056jk"
result=AddEvenPosDigs(string)
print(result)

________________________________________________________________________________________________

image screenshot

Thank You !!

Add a comment
Know the answer?
Add Answer to:
Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...
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
  • D.1 [3] Define a function called funD1...) that receives a string and returns a new string...

    D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...

  • Define a function num_letters(...) that evaluates a string consisting of numbers, letters, and symbols that returns...

    Define a function num_letters(...) that evaluates a string consisting of numbers, letters, and symbols that returns the total number of letters of the alphabet (upper and lowercase) in the string. If there are no letters in the function, then it should return the message "no way". As an example, the following code fragment: st1="aihj{234][o" print (num_letters(st1)) should produce the output: 5

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • Define a Python function named getResponse() that takes a string as its only argument and returns...

    Define a Python function named getResponse() that takes a string as its only argument and returns a new string. If the argument ends with a question mark and has an even number of characters, the function should return the string "Yes". If the argument ends with a question mark and contains an odd number of characters, the function should return the string "No". If the argument ends with an exclamation point, the function should return the string "Wow". Otherwise, the...

  • Write a function called smallestLetter that takes in a string argument and returns a char. The...

    Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32

  • 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

  • Write a function called double_str that takes a string argument and returns a new string that...

    Write a function called double_str that takes a string argument and returns a new string that has all of the same characters as the argument, except each one is repeated twice. For example: double_str('dog') --> 'ddoogg' looking for python coding below is what I have so far double_str = 'robert' double_str 'robert' for i in range(len(double_str)): print(double_str[i])

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Define a function funD2(...) which receives a list lst that contains single letters, single digits and...

    Define a function funD2(...) which receives a list lst that contains single letters, single digits and single special characters (and the list contains at least one element of each type). The function returns a string that contains the last letter and the last special character of the list, where each is repeated as many times as the sum of all digits in the list. As an example, the following code fragment: lst = ["a","b","c", 1, 2, "$","%"] print (funD2(lst)) should...

  • Python - Write a function called create_basic_pattern(background_colour, size)

    Write a function called create_basic_pattern(background_colour, size) which takes a background colour code and an integer as parameters and returns a list of strings. The list represents the pattern of a pixel art pattern. For example, consider the following code fragment:pattern \(=\) create_basicThen the function should create a list of strings. The size of the list is 8 . Each element is a string with 8 'y' characters:

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
Active Questions
ADVERTISEMENT