Question

Python Use the Design Recipe to write a function called running_average that repeatedly asks the user...

Python

Use the Design Recipe to write a function called running_average that repeatedly asks the user to input integers at the keyboard until they type the word done. Return the average of the values they entered. You may assume the user will only enter integers or "done". Include a docstring!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def running_average():
    """
    keep asking user for integers
    once done is entered, return average of all numbers entered
    :return: average of entered numbers
    """
    total = 0  # initialize total to 0
    count = 0  # initialize count to 0
    while True:
        n = input("Enter a number(or done to stop): ")  # ask user to enter a number
        if n == "done":  # if user enters done
            break  # then stop processing
        total += int(n)  # convert n to integer and add it to total
        count += 1  # increase count by 1
    if count == 0:  # if no numbers are entered
        return 0  # then return 0
    return total / count  # return calculated total


# Testing the function here. ignore/remove the code below if not required
average = running_average()
print("Average is", average)

Enter a number (or done to stop): Enter a number (or done to stop): Enter a number (or done to stop): Enter a number (or done to stop): Enter a number (or done to stop): Enter a number (or done to stop): done Average is 4.2 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python Use the Design Recipe to write a function called running_average that repeatedly asks the user...
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
  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2...

    CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2 int values greater than 0. Return whichever value is nearest to 21 without going over. Return 0 if they both go over. Include a docstring! Write three assertEqual statements to test your function.

  • python 10 pes Write a function whileloop that repeatedly asks the user for numbers, each larger...

    python 10 pes Write a function whileloop that repeatedly asks the user for numbers, each larger than the previous, and quits when the user fails to enter a larger number than the previous. You must use a while loop. No modules should be used. Your solution will be evaluated for correctness and style, and should be concise (eg, my solution is 6 lines long). You do not need to include a docstring. You do not need to check that the...

  • In Python Use the Design Recipe to write a function factorial that when given a positive...

    In Python Use the Design Recipe to write a function factorial that when given a positive integer calculates the factorial. If given a negative integer or not an integer), the function should return None. If given 0, it should return 1. Include a docstring! Note: You may assume all arguments passed to this function will be numeric. Write three assertEqual statements to test your function. To test and receive credit for your assertEqual statements, copy them into Part E and...

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

  • Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that...

    Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that consumes a list of numbers and prints a histogram graph using asterisks to represent each number in the list. Use one output line per number in the list. You may assume that only integers are passed to this function. Your function should ignore negative values. Include a docstring! Test Result print_histogram([ 0, 2, 4, 1]) ** **** * print_histogram([10, 5, 3, -1, 8]) **********...

  • Python Programming Q.3) Write a program that repeatedly asks the user to enter words until they...

    Python Programming Q.3) Write a program that repeatedly asks the user to enter words until they enter a period ("."). Your program should then print all of the words they entered including the period) on a single line separated by spaces. For example, Enter some words (. to stop): > hello > world hello world.

  • Using a Python environment, complete two small Python programs - Write a function which repeatedly reads...

    Using a Python environment, complete two small Python programs - Write a function which repeatedly reads numbers until the user enters “done” Once “done” is entered, print out the sum, average, maximum, and minimum of the values entered If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. 2. Write another function so that it draws a sideways tree pointing right; for example,...

  • Write a python function that takes in a string to use as a prompt and shows...

    Write a python function that takes in a string to use as a prompt and shows it to the user. Then get input from the user. If they entered a single sign out of the set +,-,*,/,% return it. If they entered anything else, give them a warning: “You may only enter one of the characters: +- * /%” and then repeat until they enter a correct option.

  • PYTHON Write a function called hourly_employee_input that asks the user for a name (string), hours worked...

    PYTHON Write a function called hourly_employee_input that asks the user for a name (string), hours worked (int) and an hourly pay rate (float) and prints a string including the information. Call the function, entering expected values, numbers in appropriate range Call the function, entering negative numbers Call the function, entering bad input (letters, symbols) What do you need to add to your function for bad input? Handle the bad input so your program doesn't end when receiving bad input

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