Python Forum
Python Assignment 5.2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Assignment 5.2
#1
Anyone can assist me on what's my error since I can't figure out for hours...thanks so much!!!



Error:
ParseError: bad input on line 5
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)
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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)
Reply
#4
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.
Unless explicitly silenced.

You should explicitly silence one error, not all errors.

Out of curiosity - what is wording of assignment 5.2?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for Everybody 5.2 assignment baba04201 20 173,218 Jul-25-2023, 04:15 PM
Last Post: MicaelSchutz
  Python for Everybody 3.1 assignment ramadan2099 18 45,368 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,806 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,034 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,935 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Python Password Saver Assignment sshellzr21 2 6,144 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Python for Everybody 3.3 assignment ramadan2099 7 31,722 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  Python for everyone course assignment 5.2 ofekx 3 8,525 Dec-23-2019, 08:41 PM
Last Post: nilamo
  [split] Python for Everybody 5.2 assignment jonchanzw 4 8,448 Oct-22-2019, 08:08 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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