Home > Blockchain >  Why is my code not displaying the correct max when I type in "bob" in between other number
Why is my code not displaying the correct max when I type in "bob" in between other number

Time:08-06

largest = None
smallest = None

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

        if smallest is None :
            smallest = num
        elif num < smallest :
            smallest = num
        print(smallest, num)

        if largest is None:
            largest = num
        elif num > largest:
            largest = num
            print(largest, num)

    except:
        print("Invalid input")
        continue

print('Max', largest)
print("Minimum", smallest)

Asked to find the largest and smallest number from the input but if you type in a non number it mistakes that as the max value instead of calling it a "invalid input". Help?

CodePudding user response:

It has to do with the fact that the value of "num" is being treated like a string. If you want to trigger an error exception refer to a revised version of your code with the attempted population of a numeric variable.

largest = None
smallest = None

while True:
    try:
        num = input("Enter a number: ")
        if num == "done":
            break
        print(num)
        
        x = int(num)    # Add this in to force an error exception

        if smallest is None :
            smallest = num
        elif num < smallest :
            smallest = num
        print("Smallest:", smallest, num)

        if largest is None:
            largest = num
        elif num > largest:
            largest = num
            print("Largest:", largest, num)

    except:
        print("Invalid input")
        continue

print('Max', largest)
print("Minimum", smallest)

That should provide the error condition you are after.

CodePudding user response:

The problem is that you are trying to compare strings, instead of integers. When you compare strings, Python uses lexical order to compare them.

print("100" < "2") # Prints "True", because "1" is lower than "2" in lexical order

If you compare integers instead, then Python will use numerical order, which is what you expect here.

print(100 < 2) # Prints "False", because 100 is greater than 2

To fix this, you can convert your input to a integer before comparing it. This should be done after checking whether the input equals "done", because "done" cannot be converted to an integer.

largest = None
smallest = None

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

        num = int(num)
        if smallest is None :
            smallest = num
        elif num < smallest :
            smallest = num
        print(smallest, num)

        if largest is None:
            largest = num
        elif num > largest:
            largest = num
            print(largest, num)

    except:
        print("Invalid input")
        continue

print('Max', largest)
print("Minimum", smallest)

Also, if you want to allow fractional numbers like 1.2, then you can use float instead of int.

num = float(num)

CodePudding user response:

You should specify the type of exception you're looking for, and limit the window the exception can occur in as much as possible, this helps with trying to debug situations like this:

largest = None
smallest = None

while (num := input("Enter a number: ")) != 'done':
    try:
        num = float(num)
    except ValueError:
        print("Invalid Input")
        continue
    if smallest is None:
        smallest = num
    elif num < smallest:
        smallest = num
    if largest is None:
        largest = num
    elif num > largest:
        largest = num

print('Max:', largest)
print("Minimum:", smallest)
  • Related