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.