Search code examples
pythonif-statementtry-except

Returns unexpected value


I wrote this code for an online course I am taking and you cant post code and get specific help there. I hope that someone here will help. Code -

largest = None
smallest = None
numI = 0
while True:
    num = raw_input("Prompt you")
    if num == "done":
        break
    try:
        numI = int(num)
    except:
        print "Invalid input"
        continue
    if numI >= largest or numI < smallest:
        if numI > largest:
            largest = numI
        else:
            smallest = numI 

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

Why does it return "Minimum is None"? I tried with two If loops, an If and and Elif, and now this nested loop. No matter what I dont seem to reach setting of the minimum value.

Any help greatly appreciated.

(Code is turned in and rated, so you cant ruin that part :) )


Solution

  • x < None will always return false, so in your While True: statement you need to add

    if largest==None:
        largest=numI
    if smallest==None:
        smallest=numI
    

    code:

    largest = None
    smallest = None
    numI = 0
    while True:
        num = raw_input("Prompt you")
        if num == "done":
            break
        try:
            numI = int(num)
        except:
            print "Invalid input"
            continue
        if largest==None:
            largest=numI
        if smallest==None:
            smallest=numI
        if numI > largest or numI < smallest:
            if numI > largest:
                largest = numI
            else:
                smallest = numI 
    
    print "Maximum is",largest
    print "Minimum is",smallest
    

    Note using print('Maximum is ' + str(largest)) is good practice since it is cross compatible with 3.4