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.

0

7 Answers 7

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Shahkalpesh. I did finally get a variation similar to your modified version to work!
OP's looking for an explanation, but where is it? Just a sentence or two should do.
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

Comments

1

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

2 Comments

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.
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.
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)

Comments

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

5 Comments

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.
Your welcome. If you liked the help, please "upvote" or mark answers as correct. It is how the community works :)
I don't have my 15 rep yet. I marked it up, but...it won't activate until I get 15 points.
Well then... Woo hoo!
Technically speaking, largest should be initialized to float("-inf") since it would fail for any input consisting entirely of negative numbers.
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)

3 Comments

You need to add context and comment your code. See the topic answering in the help center.
This is practically a duplicate of John's answer except that it's updated for Python 3 and uses the name num instead of inp.
Don't compare to None with ==, use is instead (link). It doesn't make a difference in this case, but it's good practice.
-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.

2 Comments

I used the logic of: if inp is None or inp < smallest: smallest=inp And that didn't work
Also like @scott said, you should also be using num and not imp because num is your parsed input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.