繁体   English   中英

最大最小中间数

[英]The biggest smallest and middle number

我有 3 个数字输入,无法弄清楚为什么中间数字不起作用。 它正在使用像 1 2 3 这样的数字但是如果您使用 go 和 150 100 50 您将得到以下结果:最大的是 150 和其他 2 50 这是代码:

a = int(input('Your first number: '))
b = int(input('Your second number: '))
c = int(input('Your third number: '))

def largest(a, b, c):
    if (a > b) and (a > c):
        largest_num = a
    elif (b > a) and (b > c):
        largest_num = b
    else:
        largest_num = c
    print('The largest number is: ', largest_num)

def smallest(a, b, c):
    if (a < b) and (a < c):
        smallest_num = a
    elif (b < a) and (b < c):
        smallest_num = b
    else:
        smallest_num = c
    print('The smallest number is: ', smallest_num)

def average(a, b, c):
    if (a > b) and (a < c):
        average_num =a
    elif (b > a) and ( b < c):
        average_num = b
    elif (c > a) and (c < b):
        average_num = c
    else:
        average_num = c
    print('The average number is: ', average_num)

largest(a, b, c)
average(a, b, c)
smallest(a, b, c)

尝试这个:

a = int(input('Your first number: '))
b = int(input('Your second number: '))
c = int(input('Your third number: '))

def largest(a, b, c):
    largest = max([a,b,c])
    print('The largest number is: ', largest)

def smallest(a, b, c):
    smallest = min([a,b,c])
    print('The smallest number is: ', smallest)

def average(a, b, c):
    if c<a<b or b<a<c:
        average_num = a
    elif a<c<b or b<c<a:
        average_num = c
    elif c<b<a or a<b<c:
        average_num = b
    else: #else all numbers are equal
        average = a
    print('The average number is: ', average_num)

largest(a, b, c)
average(a, b, c)
smallest(a, b, c)

或者你可以这样做:

a = int(input('Your first number: '))
b = int(input('Your second number: '))
c = int(input('Your third number: '))
lst = [a,b,c]
lst.sort()
print("smallest = %s" % lst[0])
print("average = %s" % lst[1])
print("maximum = %s" % lst[-1])

elif (b > a) and ( b < c):

此行仅适用于b大于a且小于c的情况。

但是如果b大于c并且小于a则它不起作用。

你可以试试:

elif ((b > a) and ( b < c) or (b < a) and ( b > c)):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM