gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:37:36 25 4
gpt4 key购买 nike

编写一个程序,要求用户输入 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 循环来解决问题,我对编程很陌生,请帮忙。

最佳答案

您可以通过在循环中进行输入来解决这个问题,然后使用内置的 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)

关于python - 编写一个程序,要求用户输入 5 个数字,并输出这些数字中最大的数字和最小的数字。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com