在没有列表的python中查找最大值和最小值

肯迪永

因此,我需要编写代码以从用户输入的一组整数中查找最小和最大数目,但是我不允许使用列表。到目前为止,这就是我的代码。

sum = 0
counter = 0
done = False 
while not done:
    integer_input = int(input("Please enter a number.")) 
    if counter < 9:
        counter += 1
        sum = sum+integer_input
    else:
        done = True 
print("The average of your numbers is:", sum/10,"The minimum of your  numbers is:" "The maximum of your numbers is:")

我可以使用for循环还是数学模块中的某些东西?

要修改代码,请执行以下操作:可以设置amin_和a 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Python中查找列表的最小值,最大值

在Python的列表模式中查找最小值和最大值

在python中查找变化集的最小值和最大值的有效方法

查找列表的最小值和最大值

没有numpy的python中矩阵的最小值和最大值

在.json中查找最大值和最小值

查找列表中每个嵌套列表的最小值和最大值

列表集合中的最小值和最大值

交换列表中的最大值和最小值

在列表中获取最小值和最大值

元组列表中的最小值和最大值

在python中查找列表的最小值和最大值。我得到了 TypeError: 'int' 对象不可调用

从数组中查找最小值和最大值,最小值始终为0

通过Python在列表中的特定位置查找绝对最大值或最小值

从 Python 中的列表列表中获取最小值和最大值的最快方法?

在python中的字典(JSON)中查找最大值和最小值

在元组python中查找字典中的最大值和最小值

从没有循环的dataFrames列表中获取最大值/最小值

在数组中查找最小值和最大值的有效方法

如何在组(SQL)中查找具有最小值和最大值的记录?

Python中的列表推导,可计算列表的最小值和最大值

在python中查找三列的最大值和最小值

Python递归-在嵌套数组中查找最大值和最小值之和

忽略列表中的 none 并在 python 中取最小值和最大值

使用 javascript 查找最小值和最大值

查找矩阵的最大值和最小值

使用awk查找最小值和最大值?

查找长向量的最小值和最大值

查找数组子集的最大值和最小值