Question

Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float...

Language: Python

Topic: Try/Except

Function name : add_divide

Parameters : list of ints, int

Returns: float

Description: Given a list of integers and a number, you want to add the numbers in the list to a total value by iterating through the given list. However, when the index you are at is divisible by the integer passed in, you must instead divide the current total by the element at that index. You must be careful when dividing the total (you can’t divide numbers by 0 or else an error will be thrown). Whenever you encounter this, instead of dividing, you must add one to the total. You also must use a try/except block for this problem. Also, round final answer to two decimal places.

Test Cases :
>>> tester1 = [9, 2, 0, 1, 0, 3, 4, 6, 5] >>> value1 = add_divide(tester1, 2) >>> print(value1)
1.6

>>> tester2 = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6] >>> value2 = add_divide(tester2, 4)
>>> print(value2)
24

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def add_divide(lst, n):
    result = 0
    for i in range(len(lst)):
        try:
            if i % n == 0:
                result /= lst[i]
            else:
                result += lst[i]
        except:
            result += 1
    return result


tester1 = [9, 2, 0, 1, 0, 3, 4, 6, 5]
value1 = add_divide(tester1, 2)
print(value1)

tester2 = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
value2 = add_divide(tester2, 4)
print(value2)
Add a comment
Know the answer?
Add Answer to:
Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float...
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
  • Language: Python Topic: API and JSON Function name: min_pop_countries Parameters: region (str), num (int) Return: list...

    Language: Python Topic: API and JSON Function name: min_pop_countries Parameters: region (str), num (int) Return: list of tuples Description: You are working on a project for your Demography class and you are tasked with finding the top num most populous countries in a given region . Instead of looking up on the Internet, you decide to apply your CS1301 knowledge of APIs and write a function to solve the problem for you. Develop a function that takes in a region...

  •    PYTHON --create a function that accepts a list of numbers and an int that returns...

       PYTHON --create a function that accepts a list of numbers and an int that returns index of 2nd occurrence of the int in list, otherwise returns None if # does not repeat more 2 or more times EX: [10,24,3,45,10,49,4,5], 10) returns 4 --create a function that accepts a string that returns true if every letter of the alphabet can be found at least one time in the string, (has to be the lowercase alphabet), and false otherwise.    for...

  • Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings),...

    Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings), completed (list of strings) Returns: final_list (list) Description : Write a function that takes in a list of tuples of strings that represents the work you have to do in each class, and a list of strings that represent the work you have already completed. Each tuple in the todo list represents the work for a single class. For this function, go through the...

  • Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays...

    Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays are 0), int (day of the month 1 to 28) Returns: dates: list of tuples Description: Imagine that you have a favorite weekday, and want to see if certain days fall on that weekday. Using the calendar module from the Python standard library , write a function which takes in a list of tuples formatted like [(month, year), etc.], your favorite weekday, and a...

  • 12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try...

    12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try the code and catch the errors. The other paradigm is 'Look before you leap' which means test conditions to avoid errors. This can cause race conditions. 1.Write the output of the code here: class MyError(Exception): pass def notZero(num): if num == 0: raise MyError def run(f): try: exec(f) except TypeError: print("Wrong type, Programmer error") except ValueError: print("We value only integers.") except Zero Division Error:...

  • Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

    Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...

  • Write a python program where you create a list (you an create it in code you...

    Write a python program where you create a list (you an create it in code you do not have to use input() statements) that represents the total rainfall for each of 12 months (assume that element 0 is January and element 11 is December). The program should calculate and display the: 1) total rainfall for the year; 2) the average monthly rainfall; 3) the months with the highest and lowest amounts. You need to figure out how to access each...

  • Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function...

    Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function variance that takes a list whose elements are numbers (floats or ints), and returns their variance, a single number. (If you don't remember how to compute variance, check the lecture notebook; it's one of the intermediate steps in computing standard deviation.) You should not use NumPy or any other Python libraries for this problem. For now, worry only about correctness, not efficiency. Passing an...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Questin 1 (CO 2) Which of the following is not a valid name in python? last_name...

    Questin 1 (CO 2) Which of the following is not a valid name in python? last_name lastName last name lname Question 2 (CO 3) How many times will “Hello World” be displayed after the following code executes? num=2 while num<12:     print("Hello world")     num+=2 2 12 5 4 Question 3 (CO 1) The following code contains an error, what type of error is it and what line number is it? 1 count=1 2 while count<4 3    print("count = ",...

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