0

The following code worked on the online course, but I'm getting errors in my IDE.

Code:

from pip._vendor.distlib.compat import raw_input

largest = None
smallest = None

while True:
    try:
        num = raw_input("enter a number: ")
        if num == "done": break

        num = int(num)

        if largest < num:
            largest = num

        if smallest is None or smallest > num:
            smallest = num

    except print as p:
        print("Invalid input")

print("Maximum is", largest)
print("Minimum is", smallest)
milanbalazs
  • 4,811
  • 4
  • 23
  • 45
Greyhound
  • 3
  • 1
  • Can you copy what the error states? And does it work when replacing ```except print as p:``` with ```except Exception as p:``` ? – bglbrt Nov 20 '19 at 10:27
  • 1
    Trying to raise an exception with ```except print as p:``` raises an error for me on Python 3, but I may be wrong? Doesn't it for you? – bglbrt Nov 20 '19 at 11:15
  • 1
    @Tibbles nevermind, the I misread my stack trace. Good spotting that out! – Mike Nov 20 '19 at 11:17
  • 1
    Yes I'm not 100% sure on that one but I think it changed with Python 3 (or at least it worked with Python 2.7), so I just thought that maybe the error was coming from this line since the author tagged the post as Python 3.x! – bglbrt Nov 20 '19 at 11:20
  • The syntax is invalid in python 2.7 as well. – Mike Nov 20 '19 at 11:28
  • Good to know!!! Sorry for the mistake then, it's been a while since I used Python 2.7. – bglbrt Nov 20 '19 at 11:37

3 Answers3

3

The answers below are good but I wanted to add explanations to make everything clearer.

num = raw_input("enter a number: ")

raw_input only exists in Python 2.x and your question is tagged Python 3.x. I assume that it matches your local python installation.

Here's a thread that goes into more depth about raw_input and input.

Another issue is this bit of code:

largest = None
smallest = None
...
if largest < num:
    largest = num

You will get an error saying:

TypeError: '<' not supported between instances of 'NoneType' and 'int'

You're essentially setting largest to a non-existent value (None marks the absence of a value) and then later you're comparing it with an integer. This is incorrect since this comparison doesn't make sense.

Fortunately, you're actually doing the right thing in the next bit of code:

if smallest is None or smallest > num:
    smallest = num

So you should either check for None in both ifs or set both values to 0 (or whatever value you think is appropriate.

Thanks to @Tibbles I've also realised you're handling the exception incorrectly. Right now you get an error saying:

Traceback (most recent call last):
  File "main.py", line 17, in <module>
    except print as p:
TypeError: catching classes that do not inherit from BaseException is not allowed

That's because print is not a type of exception.

Mike
  • 627
  • 1
  • 7
  • 21
2

Use input instead of raw_input.

Also, keep your variables in int format:

largest = 0
smallest = 0
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0

This is the error I get

TypeError: '<' not supported between instances of 'NoneType' and 'int'

Try defining largest and smallest as floats or ints and not as None.

Mike
  • 627
  • 1
  • 7
  • 21