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...

  • 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...

  • 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

  • Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe...

    Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe to write a function called blip_blop_day that consumes an int representing the number of days that have passed since their celebration start day and returns name of the current day. For example, if 2 days have passed, then 'Thursday' should be returned. Include a docstring! For example: Test Result 2 Thursday 4 Saturday Monday Tuesday 6 7 8 Wednesday

  • write in python idle Write a full python program that asks the user to type in...

    write in python idle Write a full python program that asks the user to type in 10 words using a loop, prompting the user for each word with a number. The program then displays the longest word and the shortest word the user typed in.

  • Python Write a program which asks the user keep inputting values on a line followed by...

    Python Write a program which asks the user keep inputting values on a line followed by enter until they enter a blank line, at which point the program should print the number of lines entered (excluding the blank).

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