Python Forum
Exception handler problem part 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception handler problem part 2
#1
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.
Reply
#2
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 list
How can you convert that to Python?
Reply
#3
(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.

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 list
How can you convert that to Python?
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")
Reply
#4
line 14 elif len(list1) == 0:
only needs to be else:

same with line 10
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exception handler problem Leo 10 2,900 Feb-24-2022, 08:21 PM
Last Post: deanhystad
  updating collision handler for pymunk 5.3.2 pythony 5 5,559 Oct-27-2017, 09:49 AM
Last Post: pythony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020