-4

I'm trying to write this program which asks for numbers and stores the smallest and largest in two variables which are both None at the beginning.

Somehow the largest number is stored as I want it but the smallest number never makes it.

Here's my code:

largest = None
smallest = None

while True:
    inp = raw_input("Enter a number: ")
    if inp == "done" : break

    try :
         num = int(inp)
    except :
        print "Invalid input"
        continue
    if num == None or num < smallest :
        num = smallest
    if num == None or num > largest :
        num = largest

print "Maximum is", largest
print "Minimum is", smallest

As soon as I typed in some numbers and end the program with "done" the output looks like this:

Maximum is 56
Minimum is None

I checked the indentation a couple of times.

Dave2408
  • 11
  • 3
  • When you write `num = smallest`, what do you expect to happen? Which variable gets changed? Compare it with your earlier assignment `num = int(inp)`. – Sam Mar 03 '15 at 15:44
  • 5
    you never actually assign anything to `largest` or `smallest`, in order for those to change they need to appear on the left hand side of a `=` somewhere in the code. – shuttle87 Mar 03 '15 at 15:44
  • Also, testing for `None` should be done with `is`, not with `==`, although that's not the problem here. – Tim Pietzcker Mar 03 '15 at 15:45
  • You are comparing `num` with `None` while it is `largest` and `smallest` that you set to `None` at the start. Think about that for a while. – Martijn Pieters Mar 03 '15 at 15:45
  • Your output does not match your program. As noted, neither `smallest` nor `largest` are ever assigned to (appear left of `=`) in your code snippet. – dhke Mar 03 '15 at 15:46
  • Since you never use `largest =` anywhere except to set it to `None`, your output is not feasible with the code you posted. – Martijn Pieters Mar 03 '15 at 15:46
  • In fact, I think that that is the *core* of your problem; your output **would** be produced if you did use `largest = num` and `smallest = num` in the right places, but continued to use `if num == None or ...`. Because `None` is **always smallest**, only the `if num < largest` line would ever be true to begin with. – Martijn Pieters Mar 03 '15 at 15:49

4 Answers4

2

Don't you mean :

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

instead of :

if num == None or num < smallest :
    num = smallest
if num == None or num > largest :
    num = largest

Because nothing is ever stored in smallest nor largest in the code you posted and as pointed by @MartijnPieters None is always smaller than numbers in python 2.
You can check this link : Is everything greater than None? for further information on that subject.

Also I'd prefer using explicit except such as except ValueError: in you case rather than something that catches everything.

Community
  • 1
  • 1
d6bels
  • 1,432
  • 2
  • 18
  • 30
  • 4
    This now will give an error in Python 3: `TypeError: unorderable types: int() < NoneType()`. In Python 2, it'll behave unexpectedly, because `None` is **always smaller than numbers**. You need to test `largest` and `smallest` against `None`, not `num`. – Martijn Pieters Mar 03 '15 at 15:47
  • 1
    Well the question is not tagged python 3 and the OP is using print without the new syntax so I assumed he was using python 2 – d6bels Mar 03 '15 at 15:48
  • 1
    An alternative solution would be to initialize largest as `float("-infinity")` and smallest as `float("infinity")`. – Sam Mar 03 '15 at 15:49
  • @d6bels: no, but the output the OP claimed is going to be produced by your code if run on Python 2, until you fix the comparisons. – Martijn Pieters Mar 03 '15 at 15:49
  • Absolutely but you edited the comment way too fast for me @MartijnPieters I'm fixing... ;-) – d6bels Mar 03 '15 at 15:51
0

Largest and smallest are never assigned to.

And in the spirit of writing pythonically, you can use min and max :)

Shay Nehmad
  • 1,103
  • 1
  • 12
  • 25
0
largest = None
smallest = None
a=[]

while True:
   inp = raw_input("Enter a number: ")
   if inp == "done" : break

   try :
    num = int(inp)
    a.append(num)
   except :
    print "Invalid input"
    continue

smallest=a[0]
for x in a:     
    if x<smallest:
       smallest=x

largest=max(a)
print "Maximum is", largest
print "Minimum is", smallest

You can replace the for loop with smallest=min(a)

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

@d6bels Answer is correct, but you need to add:

if smallest == None:
    smallest = num
if largest == None:
    largest = num

all code applied:

largest = None
smallest = None
a=[]

while True:
    inp = raw_input("Enter a number: ")
    if inp == "done": break
    try:
        num = int(inp)
        a.append(num)
    except:
        print("Invalid input")#For the sake of 2.x-3.x compatability, format your        
    if smallest == None:      #prints as a function
        smallest = num
    if largest == None:
        largest = num

smallest=a[0]
for x in a:     
    if x<smallest:
       smallest=x

largest=max(a)
print("Maximum is " + str(largest))
print("Minimum is " + str(smallest)) 
Ben Morris
  • 606
  • 5
  • 24