python--函数输出最大最小值

问题描述:

题目, 输入几个数然后比较大小
1.要求最少输入两个数
2.输入时可以使用逗号空格,隔开
3.可以重复多次输入
4.包装为函数

解决方案:

方法一:

def foo():
    max_ = None
    min_ = None

    while True:
        i = input(">>>")
        list_= i.replace(",", " ").split() # 注意默认以尽可能多的空白字符切割
        if list_[0] == "quit":   # 结束标志
            break
        for x in list_:
            n = int(x)
            if max_ is None:
                max_ = n
                min_ = n
            if max_ < n:
                max_ = n
            if min_ > n:
                min_ = n
        print(max_, min_)

方法二:

def foo():
    max_ = None
    min_ = None
    while True:
        new = []
        if max_ != None:
            new.extend((max_, min_))
        i = input(">>>")
        if i == "quit":
            break
        lis = [int(i) for i in i.replace(",", " ").split()]
        new.extend(lis)
        max_ = max(new)
        min_ = min(new)
        print(max_, min_)

你可能感兴趣的:(python)