用户输入的最小值和最大值

时间:2015-11-02 01:48:30

标签: python-2.7

下面的程序接受用户输入并输出数字输入的最小值和最大值,但它始终打印“无”,我找不到任何错误(显然我遗漏了一些东西)。任何人都可以帮我理解什么是错的。

largest = None
smallest = None

while True:
    num1 = raw_input("Enter a number: ")
    if num1 == "done" : break

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

        if smallest is None or num < smallest:
            smallest = num

        if largest is None or num > largest:
            largest = num

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

1 个答案:

答案 0 :(得分:2)

您的if语句缩进太多了。

largest = None
smallest = None

while True:
    num1 = raw_input("Enter a number: ")
    if num1 == "done" : break

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

    if smallest is None or num < smallest:
        smallest = num

    if largest is None or num > largest:
        largest = num

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