编写一个程序,要求用户输入5个数字,并输出这些数字中最大的数字和这些数字中最小的数字

时间:2017-05-07 17:12:16

标签: python python-3.x loops

编写一个程序,要求用户输入5个数字,并输出这些数字中最大的数字和最小数字。因此,例如,如果用户键入数字2456 457 13 999 35,则输出将如下所示: 最大的数字是2456 最小的数字是35

在Python中

a = input()
first = int(a)
b = input()
second = int(b)
c = input()
third = int(c)
d = input()
fourth = int(d)
if a > b or a > c or a > d:
  print ('the larges number' + a)
elif a < b or a < c or a < d:
  print ('the smallest number' +a )
elif b > a or b > c or b > d:
  print ('the larges number' + b)
elif b < a or b < c or b < d:
  print ('the smallest number is' + b )
elif d > a or d > b or d > c:
  print ('the biggest number is' + d )
elif d < a or d < b or d < c:
  print ('the smallest numbet is'+ d)
else:

我提出了这个想法,但我认为它不能正常工作

数字,输出最大的

这些数字和这些数字中最小的数字。

在我的模块手册中,我有这个例子

num = 0

while num < 100:

num = num + 5

print(str(num))

print(’Done looping!’)

然而,在此之后,没有进一步的解释我如何解决这个问题。你能帮我吗?

我需要通过while循环解决问题, 我是编程的新手,请帮忙。

4 个答案:

答案 0 :(得分:2)

你可以通过在循环中执行input来解决它,然后使用内置的min和max函数来显示最大和最小的:

inputs = [int(input()) for _ in range(5)]
print(min(inputs), max(inputs))

似乎你需要一个while循环,在这种情况下只需使用一个计数器:

loopcount = 0
largest = -float('inf')  # smallest possible value
smallest = float('inf')  # largest possible value
while loopcount < 5:
    loopcount = loopcount + 1

    given = input()
    given_as_integer = int(given)

    # Compare to the current smallest and largest value
    if given_as_integer < smallest:
        smallest = given_as_integer
    if given_as_integer > largest:
        largest = given_as_integer

print('the largest number is', largest)
print('the smallest number is', smallest)

答案 1 :(得分:1)

这是一个很好的方法,没有太多的代码,用户应该用space分隔数字:

a, b, c, d = map(int, input().split())
print("Largest number is {}".format(max(a, b, c, d)))
print("Smallest number is {}".format(min(a, b, c, d)))

map会将所有input更改为int,而split()会将space作为分隔符将其分解为数字。

max会将序列a, b, c, d的最大值设为min,使其成为同一序列的最小值。

如果你想进入循环,这是一种方法:

a, b, c, d = map(int, input().split())
myNumbers = [a, b, c, d]
minimum = False
maximum = False

for number in myNumbers:
    if not (minimum and maximum):
        minimum, maximum = number, number
    elif number < minimum:
        minimum = number
    elif number > maximum:
        maximum = number

print("Largest number is {}".format(maximum))
print("Smallest number is {}".format(minimum))

答案 2 :(得分:0)

我相信这就是你所追求的,享受和好运的答案。

loopnumber = 0
while loopnumber <=4:
    print('please insert 5 numbers: ', end='')
    num = input()
    loopnumber = loopnumber + 1
    if loopnumber != 1:
        if int(num) < int(smallest):
            smallest = num
        else:
            smallest = smallest
    if loopnumber != 1:
        if int(num) > int(largest):
            largest = num
        else:
            largest = largest
    else:
        smallest = num
        largest = num
print ('Done looping, smallest number is ' + str(smallest) + ", largest number is " +" "+ str(largest))

答案 3 :(得分:-1)

print("Please key in five numbers: ")
num1 = input (">")
num2 = input (">")
num3 = input (">")
num4 = input (">")
num5 = input (">")

x = max (num1, num2, num3, num4, num5)
y = min (num1, num2, num3, num4, num5)
print ("largest number is " + x)
print ("smallest number is " + y)

#you will have to add a while loop in this I think it comes for asking input 

我对编码还很陌生,所以这是我能想到的最简单和最好的解决方案