Feb-25-2022, 04:59 AM
Hello everyone, I'm still new to this so bear with me please
My issue today is that I need to create a program that continually prompts the user for a value (using a while loop). It only stops when the enter key is pressed without a value being typed in. There also must be something to catch any non-integer values like letters that are input by the user, and then it must continue to prompt the user for values. Each value that is input before the program is ended must be stored in a list and the entire list as well as some other values like the min, max, etc. must also be printed when the program is stopped (again the program stops when the enter key is pressed without also entering a number with it). This is what I have so far:
My issue today is that I need to create a program that continually prompts the user for a value (using a while loop). It only stops when the enter key is pressed without a value being typed in. There also must be something to catch any non-integer values like letters that are input by the user, and then it must continue to prompt the user for values. Each value that is input before the program is ended must be stored in a list and the entire list as well as some other values like the min, max, etc. must also be printed when the program is stopped (again the program stops when the enter key is pressed without also entering a number with it). This is what I have so far:
num = 0 list1 = [] flag = False try: while flag == False: item1 = input("Enter a value: ") item1 = int(item1) list1.append(item1) num = num + 1 except Exception: if len(item1) == 0: print("This one") flag = True print("Size of list:",num) print(list1) print("Sum:", sum(list1)) print("Maximum:", max(list1)) print("Minumum:", min(list1)) avg = num / len(list1) print("Average:", avg) else: print("Invalid input")I'm mainly looking for a better way to manage my exception handlers, but any other formatting/general tips are also greatly appreciated thanks.