因此,我需要编写代码以从用户输入的一组整数中查找最小和最大数目,但是我不允许使用列表。到目前为止,这就是我的代码。
vecF :: (Floating a, VectorSpace v) => v a -> [v a] -> [a]
vecF v0 vs = [ vecMagnitude (vecDiff v0 v1) | v1 <- vs ]
是否可以使用for循环或数学模块中的某些内容?
答案 0 :(得分:0)
要修改代码,请执行以下操作:可以设置min_
和max_
,然后每次比较输入的整数。如果输入小于min_
,则覆盖该变量。如果它大于max_
,则覆盖该变量:
sum_ = 0
counter = 0
done = False
while not done:
integer_input = int(input("Please enter a number."))
if counter == 0:
min_ = integer_input
max_ = integer_input
if counter < 9:
if integer_input < min_:
min_ = integer_input
if integer_input > max_:
max_ = integer_input
counter += 1
sum_ = sum_+integer_input
else:
done = True
print("The average of your numbers is:{}, The minimum of your numbers is: {}, The maximum of your numbers is: {}".format(sum_/10, min_, max_))
示例输入:
Please enter a number.1
Please enter a number.2
Please enter a number.3
Please enter a number.4
Please enter a number.5
Please enter a number.6
Please enter a number.5
Please enter a number.4
Please enter a number.3
Please enter a number.2
>>> print("The average of your numbers is:{}, The minimum of your numbers is: {}, The maximum of your numbers is: {}".format(sum_/10, min_, max_))
The average of your numbers is:3.3, The minimum of your numbers is: 1, The maximum of your numbers is: 6