2

I'm trying to create an program in python for a class and I can't figure out why I can't get the expected output. The program is to take user input values until a 'done' is entered, and then print the max and min of these values. It also has some error checking for valid input (number and not text).

largest = None
smallest = None
while True:
    inp = raw_input("Enter a number: ")
    if inp == "done" : 
        break
    try:
        num=float(inp)
    except:
        print "Invalid input"
        continue

    if inp>largest: 
        largest=inp
    if inp<smallest: 
        smallest=inp

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

The loop breaks properly if 'done' is inserted. It doesn't fail if a text string is entered, but also doesn't print "Invalid input". I'm not asking for someone to solve my homework program, but to provide me with an explanation as to why I never get largest or smallest to be anything other than their original assignment of "None".

Thanks in advance.

akaDrHouse
  • 2,190
  • 2
  • 20
  • 29

7 Answers7

2
largest = None
smallest = None
num = None

while True:
    inp = raw_input("Enter a number: ")
    if inp == "done" : 
        break
    try:
        num=float(inp)
    except:
        print ("Invalid input")
        continue

    if largest is None:
        largest = num
        smallest = num

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

print ("Maximum is ", largest)
print ("Minimum is ", smallest)
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
2
if smallest is None or smallest< inp:
    smallest=inp
if largest is None or largest > inp:
    largest=inp

You need to do something with your comparison operators bro. Right now you're checking if smallest is smaller than input (and vice versa for largest). The result: you're switching max and min. Should've been like this:

if smallest is None or inp < smallest:
    smallest = inp
if largest is None or inp > largest:
    largest = inp
1

It doesn't report the proper max or min because you use inp (the string) in your comparisons instead of num (the float).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I'm brand new to Python, so I am likely wrong. Scott, I don't think that is correct. The try:except statement checks for correct input. If that fails, it starts over. If it does't fail, then the input must have been a number and thus valid for comparison. Please let me know if that isn't correct. – akaDrHouse Oct 12 '15 at 20:09
  • 1
    This is not so much incorrect as incomplete (see the accepted answer); comparing different types doesn't necessarily raise an exception, but probably doesn't produce the answer you want. – Scott Hunter Oct 12 '15 at 21:08
1

This is the final code I used. With help from all of you that responded! Thanks to all for their time.

largest = None
smallest = None

while True:
    inp = raw_input("Enter a number: ")
    if inp == "done" : 
        break
    try:
        num=float(inp)
    except:
        print ("Invalid input")
        continue

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

print ("Maximum is ", largest)
print ("Minimum is ", smallest)
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
0

Don't use None as your initialization, use floats.

largest =float("-inf") # negative infinity
smallest = float("inf") # infinity
while True:
    inp = raw_input("Enter a number: ")
    if inp == "done" : 
        break
    try:
        num = float(inp)
        largest = max(largest, num)
        smallest = min(smallest, num)
    except:
        print "Invalid input"
        continue

print "Maximum is ", largest
print "Minimum is ", smallest
RobertB
  • 1,879
  • 10
  • 17
  • 1
    Thank you Robert. I got your code to work by then converting the largest smallest to integers before printing. I also finally figured out how to use the None function by utilizing : if smallest is None or smallest< inp: smallest=inp if largest is None or largest > inp: largest=inp Thanks to all who helped. – akaDrHouse Oct 12 '15 at 19:28
  • Your welcome. If you liked the help, please "upvote" or mark answers as correct. It is how the community works :) – RobertB Oct 12 '15 at 19:34
  • I don't have my 15 rep yet. I marked it up, but...it won't activate until I get 15 points. – akaDrHouse Oct 12 '15 at 19:38
  • Well then... Woo hoo! – RobertB Oct 12 '15 at 19:39
  • 2
    Technically speaking, `largest` should be initialized to `float("-inf")` since it would fail for any input consisting entirely of negative numbers. – Josh J Oct 12 '15 at 19:46
0
largest = None
smallest = None

while True:
    num = input("Enter a number: ")
    if num == "done":
        break

    try:
        num = int(num)
    except:
        print("Invalid input")
        continue

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

print("Maximum is", largest)
print("Minimum is", smallest)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
niksy
  • 323
  • 1
  • 4
  • 9
  • You need to add context and comment your code. See the topic [answering](https://stackoverflow.com/help/how-to-answer) in the help center. – Sumi Straessle Jul 23 '17 at 17:10
  • This is practically a duplicate of [John's answer](https://stackoverflow.com/a/35154640/4518341) except that it's updated for Python 3 and uses the name `num` instead of `inp`. – wjandrea Sep 16 '20 at 04:44
  • Don't compare to `None` with `==`, use `is` instead ([link](https://stackoverflow.com/q/14247373/4518341)). It doesn't make a difference in this case, but it's good practice. – wjandrea Sep 16 '20 at 04:51
-1

It works, kinda

python27 test.py
Enter a number: 10
Enter a number: 20
Enter a number: 40
Enter a number: done
Maximum is  40
Minimum is  None

You will never get a min because a float is never less than None.

Josh J
  • 6,813
  • 3
  • 25
  • 47