84.1k views
9 votes
Implement a function inValues() that asks the user to input a set of nonzero floating-point values. When the user enters a value that is not a number, give the user a second chance to enter a value. After two mistakes in a row, quit the program. When the user enters 0, the function should return the sum of all correctly entered values. Use exception handling to detect improper inputs.

User Jokin
by
4.7k points

1 Answer

7 votes

Answer:

Step-by-step explanation:

The following is written in Python and uses exception handling to do exactly as requested. It then goes adding all of the integer values to an array called num_list and finally adding them all together when the function ends.

def in_values():

num_list = []

while True:

try:

num = input("Input non-zero floating point: ")

num = int(num)

if num == 0:

break

else:

num_list.append(num)

except ValueError:

print("No valid integer! Please try again ...")

try:

num = input("Input non-zero floating point: ")

num = int(num)

break

except ValueError:

break

sum = 0

for number in num_list:

sum += number

return sum

User Austin Salonen
by
4.4k points