2

I don't understand why this doesn't work. I want to print the highest negative value from a series of user-inputted negative ints. E.g., user inputs: -1, -5, -3, program returns -1. But my program (below) is returning -5. Why is this? Is my code totally messed up? I know I can use a list and max way around it but I don't want to over-complicate the program.

x = 0
done = False
while not done:
    y = int(input("Enter another number (0 to end): "))
    num = y
    if num != 0:
        if num < x:
            x = num
    else:
        done = True
print(str(x))
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
CoderBaby
  • 91
  • 2
  • 8
  • Because -5 < -1? That's what the highest negative value means. Do you want the smallest then? Reverse your operations – TerryA Jun 27 '17 at 12:39
  • 1
    You are setting x to the lowest number, not the highest. Reverse the comparator in `if num < x`. – Mrl0330 Jun 27 '17 at 12:39
  • 3
    " I know I can use a list and max way around it but I don't want to over-complicate the program" *Your* way over-complicates the program. Using `max` would be a single line of code. – DeepSpace Jun 27 '17 at 12:40

4 Answers4

4

Your operator should be greater than >, not less than < in order to take the maximum value. Initializing to -float('inf') ensures the first negative value passes the condition:

x = -float('inf')
while True:
    num = int(input("Enter another number (0 to end): "))
    if num != 0:
        if num > x:
            x = num
    else:
        break
print(x)

You can drop the done variable by using a while True...break instead.


I know I can use a list and max way around it but I don't want to over-complicate the program.

You can do this in a single line using iter with your sentinel 0 to call input repeatedly, collecting an iterable of negative numbers. map(int, ...) converts the iterable items to ints while max returns the maximum:

max(map(int, iter(input, '0')))

Demo:

>>> m = max(map(int, iter(input, '0')))
-3
-1
-4
-2
0
>>> m
-1
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
3

Well the highest negative value is the same as the maximum value.

Now your loop invariant should be that x is the thus far observed maximum value. But you actually store the thus far observed minimum value: indeed if the new value is less than, you assign it to x.

So a quick fix is to change to comparison to >. But now the initial maximum will be 0. We can change that, by setting the initial value to for instance None, and if x is None, set x to the entered value.

x = None
done = False
while not done:
    y = int(input("Enter another number (0 to end): "))
    num = y
    if num != 0:
        if x is None or num > x:
            x = num
    else:
        done = True
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Good solution. Thank you! I was not familiar with using None but I will remember it now for future reference. – CoderBaby Jun 30 '17 at 09:27
1

You never compare the inputted value with the largest-negative-value so far. You also set your initial value to zero, which is not a suitable result value. One way to handle these is to replace your lines

if num < x
    x = num

with

if num < 0 and (x == 0 or x < num < 0):
  x = num

There are other ways, of course, including setting x to the smallest possible negative number. That would simplify your comparisons, since in my code above there is a check just for x never being set before.

Note that if there is no negative number input at all, the result is zero. That may or may not be what you want.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
1

Just use inbuilt max function to find maximum number

numbers = []
done = False
while not done:
    number = int(input("Enter another number (0 to end): "))
    if number < 0:
        numbers.append(number)
    else:
        done = True

print(max(numbers)) 
hspandher
  • 15,934
  • 2
  • 32
  • 45