I have an activity for some practice for a Python class. The aim is to have unlimited amount of input from the user and assuming user is inputting correct data. The input needs to end when a blank line is detected. (this is what seems to have me stumped, trying to avoid a Value Error when doing this.) After input ends I need to sort numbers and find the total of numbers and average of numbers.
numbers = []
num_input = []
tot_numbers = []
while num_input != "":
try:
num_input = int((input("Input a number: ")))
numbers.append(num_input)
numbers.sort()
print(numbers)
except:
pass
print("Sorted numbers: ", numbers)
tot_numbers = sum(numbers)
print("Total of numbers: ", tot_numbers)
avg_numbers = tot_numbers / len(numbers)
print("Average of numbers: ", avg_numbers)
print("Finished.")
break
This code above is what I have come to and it works but I am not too happy with it because of using 'except'. I know there is a better way and that it probably uses an if statement within the while loop, I have been playing around with something like:
if num_input.isnumeric():
but I get an AttributeError because I can't check if a list is numeric, any help would be greatly appreciated, thankyou!