Write a function called 'average' that receives a list of numbers like parameter and return the average of those numbers. The list that receives the function must be entered by the user, but it must be validated that only numbers are entered. Whether enter a different value, the function should print 'ERROR' on the screen.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Write a function called 'average' that receives a list of numbers like parameter and return the average of those numbers. The list that receives the function must be entered by the user, but it must be validated that only numbers are entered. Whether enter a different value, the function should print 'ERROR' on the screen.
Expert Solution
Step 1: Providing Algorithm of code
  1. Define a function called 'average' that takes a list of numbers as a parameter.

    • Inside the 'average' function: a. Initialize a try-except block to handle potential ValueErrors. b. Attempt to convert each element in the input list to a float. c. Calculate the average by summing the numbers and dividing by the count. d. If successful, return the calculated average. e. If a ValueError occurs (e.g., non-numeric input), return 'ERROR'.
  2. Define another function called 'get_numeric_input' that doesn't take any parameters.

    • Inside the 'get_numeric_input' function: a. Initialize an empty list called 'num_list' to store the numeric input. b. Start an input loop: i. Prompt the user to enter a number (or 'done' to finish). ii. Read the user's input. iii. If the user input is 'done,' exit the loop. iv. Try to convert the user input to a float. v. If successful, add the number to 'num_list.' vi. If a ValueError occurs, print 'ERROR: Please enter a valid number.' c. Return 'num_list' containing the numeric input.
  3. Get user input by calling the 'get_numeric_input' function.

    • This function prompts the user to enter numbers and validates their input.
  4. Calculate the average by calling the 'average' function with the list of numbers obtained from step 3.

    • Check if the result is 'ERROR.'
  5. If the result is 'ERROR,' print 'ERROR: Invalid input detected.'

    • This occurs if non-numeric input was detected during data entry.
  6. If the result is a valid average, print the calculated average on the screen.

  7. End.

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education