Question

In: Computer Science

Using Python def specialAverage():    Print the number of 0s and then return the average of...

Using Python

def specialAverage():
  
Print the number of 0s and then return the average of either the positive
values in the list (if the optional parameter has value 1) or the negative
values in the list (if the optional parameter has value 0).
  
Arguments:
a list of numbers : the list can contain any numeric values
an integer : this integer is an optional parameter and only takes value
0 or 1, and defaults to 1 if the user does not specify the value
when calling the function.
  
Return:
a number : the computed average of either positive or negative numbers
rounded to 1 decimal place
  
>>> specialAverage([1,2,3,4,-1,-2,-3,-4],0)
-2.5
and also prints "There are 0 zeros in the list"

Solutions

Expert Solution

def specialAverage(lst, choice=1):
    """
    Print the number of 0s and then return the average of either the positive
    values in the list (if the optional parameter has value 1) or the negative
    values in the list (if the optional parameter has value 0).

    Arguments:
    a list of numbers : the list can contain any numeric values
    an integer : this integer is an optional parameter and only takes value
    0 or 1, and defaults to 1 if the user does not specify the value
    when calling the function.

    Return:
    a number : the computed average of either positive or negative numbers
    rounded to 1 decimal place
    """
    pos_total, pos_count, neg_total, neg_count, zero_count = 0, 0, 0, 0, 0
    for num in lst:
        if num > 0:
            pos_count += 1
            pos_total += num
        elif num < 0:
            neg_count += 1
            neg_total += num
        else:
            zero_count += 1
    print("There are {} zeros in the list".format(zero_count))
    if choice == 1:
        return pos_total / pos_count
    else:
        return neg_total / neg_count


print(specialAverage([1, 2, 3, 4, -1, -2, -3, -4], 0))


Related Solutions

Python code def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2...
Python code def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user defined by having the most interests in common. Return a tuple: (most_similar_user, num_interests_in_common) """ return ("", 0)    def recommendations(user, interests): """ Find the user who shares the most interests with the specified user. Recommend to user any interests that...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def countyTax(amount): taxRate = 0.025 taxAmount = taxRate * amount print('County tax: ',taxAmount) return taxAmount def totalTax(state,county): taxAmount = state + county print('Total tax: ',taxAmount) return taxAmount def totalAmount(amount, tax): total = amount + tax return total def main(): amount = float(input('Enter the amount of purchase: ')) sTax = stateTax(amount) cTax = countyTax(amount) tTax = totalTax(sTax,cTax) print('Total amount including tax is: ' ,total) main() I...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like this. 1) create a function popular. The dictionary called database contains each people's hobby and their excitement level. This function searches the dictionary and returns a list of that most popular hobby to least popular hobby. If multiple hobbies have the same number of popularity, follow alphabetical order. #What is popular means? The dictionary contains each people's hobby. The programmer should search the dictionary...
IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me"...
IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me" and "Loves me not" for every alternating petal. The last phrase printed (for the last petal picked) should be in all caps. Remember to include a comma and space between phrases, as shown in examples below. Sample Input 1 3 Sample Output 1 "Loves me, Loves me not, LOVES ME" Sample Input 2 1 Sample Output 2 "LOVES ME"
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that sum up to 'sum' by inserting the operators between digits in 'number'. example for 'number=123456789' and 'sum = 0' Print the output using the terminal: Output should be exactly like this from 1 - 22 1 : +1+2-34-56+78+9=0 2 : +1-2-34+5+6+7+8+9=0 3 : +1-23-4-56-7+89=0 ... 12 : -1+2+34-5-6-7-8-9=0 13 : -1+23+4+56+7-89=0 14 : -1-2+34+56-78-9=0 ... 22 : -12-34+56+7-8-9=0
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT