Reputation: 3
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)
Upvotes: 0
Views: 49
Reputation: 173
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
.
Upvotes: 0
Reputation: 635
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 if
s 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.
Upvotes: 3
Reputation: 1837
Use input
instead of raw_input
.
Also, keep your variables in int format:
largest = 0
smallest = 0
Upvotes: 2