最大和最小的数字。蟒蛇

时间:2016-11-18 17:10:45

标签: python

我知道它非常简单,但我很难找到python中三个数字中最大和最小的数字。 这是我的代码: *我不允许使用内置函数min和max,所以我必须使用if和else ...)*

def largest_and_smallest(number1, number2, number3):
    if number1 >= number2:
        maxn=number1
        minn=number2
        if number1 >= number3:
           maxn=number1
            if number3>=number2:
                minn=number2
            else:
                minn=number3
        else:
            maxn=number3
            minn=number2
    else:
        maxn=number2
        minn=number1
        if number2 >= number3:
             maxn=number2
             if number3 >= number2:
                 minn=number2
             else:
                 minn=number3
         else:
             maxn=number3
             minn=number1
    print(maxn,minn)

我得到了几个错误消息,例如:未使用局部变量maxn和minn以及外部作用域的阴影名称minn和maxn。我很新,所以我很乐意得到一些帮助。 非常感谢提前!

8 个答案:

答案 0 :(得分:2)

除了在您将代码粘贴到问题中时引入的小缩进问题之外,您的代码正常工作。你得到的“错误”不是错误;它们是来自IDE的警告,您可以放心地忽略它们。

答案 1 :(得分:2)

您可以使用简单的sorting network执行此操作。

def sort3(a, b, c):
    if c < b: b, c = c, b
    if b < a: a, b = b, a
    if c < b: b, c = c, b
    return a, b, c

# demo

data = [
    (1, 2, 3),
    (1, 3, 2),
    (2, 1, 3),
    (2, 3, 1),
    (3, 1, 2),
    (3, 2, 1),
]

for row in data:
    print(sort3(*row))

<强>输出

(1, 2, 3)
(1, 2, 3)
(1, 2, 3)
(1, 2, 3)
(1, 2, 3)
(1, 2, 3)

正如Simon在评论中提到的,我的sort3函数不是用通常的Python风格编写的。这是符合PEP-0008的形式。

def sort3(a, b, c):
    """ Sort (a, b, c) using a Sorting Network """
    if c < b: 
        b, c = c, b
    if b < a: 
        a, b = b, a
    if c < b: 
        b, c = c, b
    return a, b, c

答案 2 :(得分:0)

选项1:

def largest_and_smallest(number1, number2, number3):
    print(max([number1, number2, number3]))
    print(min([number1, number2, number3]))

largest_and_smallest(1, 3, 2)

选项2:列表:

def largest_and_smallest(lst):
    print(max(lst))
    print(min(lst))


largest_and_smallest([1, 2, 3, 4])

答案 3 :(得分:0)

def largest_and_smallest(number1, number2, number3):
    if number1 > number2 and number1 > number3:
        maxn=number1
    elif number2 > number1 and number2 > number3:
        maxn=number2
    else:
        maxn=number3
    if number1 < number2 and number1 < number3:
        minn=number1
    elif number2 < number1 and number2 < number3:
        minn=number2
    else:
        minn=number3
    print maxn,minn


largest_and_smallest(1,2,3)

答案 4 :(得分:0)

如果总共提交了3个整数,那么我只需对列表进行排序并使用第一个和最后一个索引。 由于您不允许使用最小值和最大值我使用已排序

def largest_and_smallest(number1, number2, number3):
    arr = sorted([number1, number2, number3])
    n_min, n_mid, n_max = arr[0], arr[1], arr[2]
    return n_min, n_max

修改我对条件要求的回答

def largest_and_smallest(number1, number2, number3):
    high = 0
    low = 0
    if (number1 > number2) and (number1 > number3):
        high = number1
    elif (number2 > number1) and (number2 > number3):
       high = number2
    else:
       high = number3

    if (number1 < number2) and (number1 < number3):
        low = number1
    elif (number2 < number1) and (number2 < number3):
       low = number2
    else:
       low = number3

    print(low, high)

largest_and_smallest(10, 100, 1)
largest_and_smallest(1, 2, 3)

答案 5 :(得分:0)

将它们存储在列表中以使自己更容易,然后使用内置的Python函数来提取最小值和最大值。

nums = [1,3,2]

print(max(nums))
print(min(nums))

> 3
> 1

Built in functions

List, Arrays, and Other Data Structures

编辑:我看到你不允许使用内置函数,这将返回一个基于值数组的(min,max)元组。

nums = range(1,100)

def min_max(vals):
  min_val = vals[0]
  max_val = vals[0]

  for v in vals:
    if v > max_val:
      max_val = v
    if v < min_val:
      min_val = v

  return (min_val, max_val)

print(min_max(nums))

答案 6 :(得分:0)

你可以使它更加密集,增加可读性并仍然遵循通常的python约定,并且不扩展它以允许任意多个整数..

def min_and_max(numbers):
    min_num, max_num, *_ = numbers
    for num in numbers:
        max_num = num if num > max_num else max_num
        min_num = num if num < min_num else min_num
    return min_num, max_num


def main():
    data = (
        (1, 2, 3),
        (-20, 5, 100),
        (4, 23, 2, 3, 1),
        (123, -200, 3, 232),
        (123, 123, 123),
        (13, 21, 31, 1, 1, 1, 0)
    )
    for nums in data:
        print(min_and_max(nums))


if __name__ == '__main__':
    main()

给出:

(1, 3)
(-20, 100)
(1, 23)
(-200, 232)
(123, 123)
(0, 31)

如果你不允许使用list等,那么,使用python处理参数的原因,你的老师肯定不知道你要改变什么python。

你可以简单地告诉python不要解压你的峰值参数,而是将它们保存在输入的形式中。

def min_and_max(*args):
    min_num, max_num, *_ = args
    for num in args:
        max_num = num if num > max_num else max_num
        min_num = num if num < min_num else min_num
    return min_num, max_num


def main():
    data = (
        (1, 2, 3),
        (-20, 5, 100),
        (4, 23, 2, 3, 1),
        (123, -200, 3, 232),
        (123, 123, 123),
        (13, 21, 31, 1, 1, 1, 0)
    )
    for nums in data:
        print(min_and_max(*nums))


if __name__ == '__main__':
    main()

答案 7 :(得分:-1)

问题是作用域中没有定义maxn和minn,尝试插入这些行我的代码来初始化maxn和minn变量:

maxn=0
minn=0

所以现在你的代码看起来像这样:

def largest_and_smallest(number1, number2, number3): 

    #initializing vars to zero
    maxn=0
    minn=0

    if number1 >= number2: 
         maxn=number1
         #rest of you code...

这应修复本地变量警告,有关范围的更多信息,请参阅Python Docs