Python Assignment 5.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: Python Assignment 5.2 (/thread-15432.html) |
Python Assignment 5.2 - amos76823 - Jan-17-2019 Anyone can assist me on what's my error since I can't figure out for hours...thanks so much!!!
largest = None smallest = None while True: inp = raw_input('Enter a number: ') if inp == 'done': break try: num = int(inp) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print('Invalid input') continue print("Maximum is", largest) print("Minimum is", smallest) RE: Python Assignment 5.2 - perfringo - Jan-17-2019 First observation is that if statement should not be intended. It should be on same level as inp. inp = raw_input('Enter a number: ') if inp == 'done': break RE: Python Assignment 5.2 - amos76823 - Jan-17-2019 it works after I change to below. Thanks for your feedback! largest = None smallest = None while True: inp = raw_input('Enter a number: ') if inp == 'done': break try: num = int(inp) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print('Invalid input') continue print("Maximum is", largest) print("Minimum is", smallest) RE: Python Assignment 5.2 - perfringo - Jan-17-2019 I advise to be specific about errors. You don't want silence all errors in your code. You want to handle only errors arising from user input which can't be converted to int. So my suggestion to change row 15 to this: except ValueError:From The Zen of Python: Quote:Errors should never pass silently. You should explicitly silence one error, not all errors. Out of curiosity - what is wording of assignment 5.2? |