Exercise 5.1: Write a program which repeatedly reads numbers until the user
enters "done". Once "done" is entered, print out the total, count, and average
of the numbers. 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.
Enter a number: 4
Enter a number: 5
Enter a number: bad data
Invalid input
Enter a number: 7
Enter a number: done
16 3 5.33333333333333
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
# Loop Exercise#1
count = 0
sums = 0
avg = 0
while True :
inp_num = input("Enter a number: ")
try :
inp_num = int(inp_num)
count = count + 1
except :
if inp_num == "done":
break
print("invalid input !!!")
continue
sums = sums + inp_num
avg = sums / count
print(sums, count, avg)
quit()
'PYTHON' 카테고리의 다른 글
Answer (5-02) for Python for Everybody by Charles R Severance (0) | 2019.09.23 |
---|---|
Answer (4-07) for Python for Everybody by Charles R Severance (0) | 2019.09.23 |
Answer (4-06) for Python for Everybody by Charles R Severance (0) | 2019.09.23 |
Answer (3-03) for Python for Everybody by Charles R Severance (0) | 2019.09.23 |
Answer (3-02) for Python for Everybody by Charles R Severance (0) | 2019.09.23 |