[英]The biggest smallest and middle number
I have 3 inputs of numbers and cannot figure out why the middle number is not working.我有 3 个数字输入,无法弄清楚为什么中间数字不起作用。 It is working with numbers like 1 2 3 But if you go with 150 100 50 you will get the following result: biggest is 150 and other 2 50 Here is the code:它正在使用像 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)
try this:尝试这个:
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)
or you could just do:或者你可以这样做:
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):
This line only works in the case that b
is bigger than a
and smaller than c
.此行仅适用于b
大于a
且小于c
的情况。
But it doesn't work if b
is bigger than c
and smaller than a
.但是如果b
大于c
并且小于a
则它不起作用。
You can try:你可以试试:
elif ((b > a) and ( b < c) or (b < a) and ( b > c)):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.