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).
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return x ** 3 ##def function_3( x ) : return x ** 4 ## ###### create a list of callbacks to each of the functions ######by referencing their names ## ##callbacks = [ function_1 , function_2 , function_3 ] ## ######display a heading and the result of passing a value to each of the ######named functions: ## ##print( '\nNamed Functions:' ) ##for function in callbacks...
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...
***This program is to be created using Python*** Requirements to be numbered in program01.py: Print “This...
***This program is to be created using Python*** Requirements to be numbered in program01.py: Print “This program provides concert seat assignments.” Ask the user their musical preference and take the following actions based on their potential input: Classical – proceed with the program (i.e. move the next requirement) – output a message that states “We do not recognize the musical genre.” Then end the program. Ask the user to enter the following data about themselves: • First Name • Last...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT