Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the count, sum and average of the numbers 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.

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

Python Programming 

Programming Demonstrations
Programming Demo 2: Count, Sum and Mean
Write a program which repeatedly reads numbers until the user enters "done". Once
"done" is entered, print out the count, sum and average of the numbers 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.
1. Type the following code on your Python script.
1 count = 0
2 total = 0
3 while True:
4
try:
inp = input('Enter a number: )
if inp == 'done':
7
break
8
num = float(inp)
count = count + 1
10
total = total + num
11
mean = total/count
12
except:
13
print("\nlnvalid input!')
14
continue
15 print('Count = (0), Sum = {1:2f), Average = (2:2f)' format(count,total,mean))
2. Run the script on the Python console and use the program. Try different inputs and
check the result. Also try to enter some non-numerical inputs to test the try/except
block of the code.
n 67
Transcribed Image Text:Programming Demonstrations Programming Demo 2: Count, Sum and Mean Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the count, sum and average of the numbers 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. 1. Type the following code on your Python script. 1 count = 0 2 total = 0 3 while True: 4 try: inp = input('Enter a number: ) if inp == 'done': 7 break 8 num = float(inp) count = count + 1 10 total = total + num 11 mean = total/count 12 except: 13 print("\nlnvalid input!') 14 continue 15 print('Count = (0), Sum = {1:2f), Average = (2:2f)' format(count,total,mean)) 2. Run the script on the Python console and use the program. Try different inputs and check the result. Also try to enter some non-numerical inputs to test the try/except block of the code. n 67
3. Explain lines 6 and 7. What happens to the program when this if block is executed?
4. Explain what happens to lines 9, 10 and 11 every time the while block is executed?
5. When an error occurs within the try block, the program will proceed to execute the
except block as discussed in the previous experiments. Explain the statements within
the except block and briefly discuss what it does in the execution of the program.
Write your notes here:
Transcribed Image Text:3. Explain lines 6 and 7. What happens to the program when this if block is executed? 4. Explain what happens to lines 9, 10 and 11 every time the while block is executed? 5. When an error occurs within the try block, the program will proceed to execute the except block as discussed in the previous experiments. Explain the statements within the except block and briefly discuss what it does in the execution of the program. Write your notes here:
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
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