小编Tal*_*ved的帖子

Python:无需使用max和min函数即可获得最大值和最小值的程序

我编写了下面的代码,用于获取最大值和最小值作为我的MOOC分配的一部分.该程序不断地从用户接收输入,直到用户键入"完成".

输入'done'后,程序会给出最大值和最小值的结果.问题是最大值的结果总是正确的,但最小值的结果总是"无".

largest = None
smallest = None
while ( True ) :
    inp = raw_input('Enter a number: ')
    if inp == 'done' :
        break
    try:
        inp = float(inp)
    except:
        print 'Invalid input'
    continue
    if inp is None or inp > largest:
        largest = inp
    if inp is None or inp < smallest:
        smallest = inp
print largest, smallest
Run Code Online (Sandbox Code Playgroud)

python-2.7

5
推荐指数
1
解决办法
1884
查看次数

标签 统计

python-2.7 ×1