Exception handler problem part 2 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Exception handler problem part 2 (/thread-36490.html) |
Exception handler problem part 2 - Leo - Feb-25-2022 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: 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. RE: Exception handler problem part 2 - deanhystad - Feb-25-2022 Be consistent with your indentation (always use 4 spaces). Inconsistent indenting makes your code hard to read. An exception takes you out of the while loop. try: while flag == False: item1 = input("Enter a value: ") item1 = int(item1) # If an exception happens here the program jumps down to line 8 list1.append(item1) num = num + 1 # This is the last command in the while loop. except Exception: if len(item1) == 0: # The program jumps here if an exception occurs. This is not in the while loop. print("This one")You don't know Python and you are letting that affect how you write programs. Coding in a particular language is a small part of the programming task. Most of programming is coming up with a good algorithm for solving the problem. You need to focus on the algorithm first, then think about translating the algorithm to Python. Your program needs to do this: repeat get user input if input is not empty convert input to a number and add it to a list until input is empty if list is not empty print max value of list print min value of list do other things involving the listHow can you convert that to Python? RE: Exception handler problem part 2 - Leo - Feb-26-2022 (Feb-25-2022, 05:44 AM)deanhystad Wrote: Be consistent with your indentation (always use 4 spaces). Inconsistent indenting makes your code hard to read.Ok so I believe I have done what you said and it seems to work well. However I still have one remaining problem. My professor makes a big deal of how when a non-integer is input(namely a letter), the program must continue to ask for a value. Only when the input is "" will it stop the program and print out all of the math functions like min, max, mean etc. this is what I have: list1 = [] num = 0 flag = False try: while flag == False: item1 = input("enter a value: ") if item1 != "": item1 = int(item1) list1.append(item1) elif item1 == "": flag = True if len(list1) != 0: print("all the math shit goes here!") elif len(list1) == 0: print("Also print the math shit here!") except Exception: print("oof") RE: Exception handler problem part 2 - Larz60+ - Feb-27-2022 line 14 elif len(list1) == 0: only needs to be else: same with line 10 |