【问题标题】:Finding minimum and maximums in python without lists在没有列表的python中查找最小值和最大值
【发布时间】:2019-03-17 22:19:00
【问题描述】:

所以我需要编写一个代码来从用户输入的一组整数中找到最小和最大数字,但我不能使用列表。这就是我的代码到目前为止的样子。

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 循环或数学模块中的某些东西?

【问题讨论】:

标签: python-3.x max user-input minimum


【解决方案1】:

修复你的代码:你可以设置一个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

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2017-09-19
    • 2015-06-29
    • 2015-01-16
    • 1970-01-01
    • 2015-09-29
    • 2022-06-16
    相关资源
    最近更新 更多