Python Forum
Exception handler problem part 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception handler problem part 2
#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


Messages In This Thread
Exception handler problem part 2 - by Leo - Feb-25-2022, 04:59 AM
RE: Exception handler problem part 2 - by deanhystad - Feb-25-2022, 05:44 AM
RE: Exception handler problem part 2 - by Leo - Feb-26-2022, 09:18 PM
RE: Exception handler problem part 2 - by Larz60+ - Feb-27-2022, 12:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Exception handler problem Leo 10 3,092 Feb-24-2022, 08:21 PM
Last Post: deanhystad
  updating collision handler for pymunk 5.3.2 pythony 5 5,620 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