Search code examples
python-3.xevaluation

Python - 10 evaluating lower than 2


I've added some troubleshooting code to try and figure out what's going on, but what keeps happening is when entering the numbers specified in the Py4E Exercise, particularly when I get to 10, it says 10 is my smallest number. I've tried with a variety of numbers, but I'm just not getting why it thinks 10 is smaller than 2. Maybe it's something obvious, but please help. (Input entered: 7, 2, bob, 10, and 4)

largest = None
smallest = None
while True:
    print('largest:', largest)
    print('smallest:', smallest)
    num = input('Enter a number: ')
    if num == 'done':
        break

    try :
        int(num)
    except :
        print('Invalid input')
        continue

    if largest == None and smallest == None :
        print('if largest == None and smallest == None :', largest == None and smallest == None)
        print("Setting value of largest to", num)
        largest = num
        print("Setting value of smallest to", num)
        smallest = num
    elif largest < num :
        print("Largest < num")
        largest = num
    elif smallest > num :
        print("smallest > num")
        smallest = num
    else :
        print("Something else")
    print(num)

print("Maximum", largest)
print("Minimum", smallest)

Solution

  • int(num) converts num to an integer which is then discarded. You want num = int(num).