Нахождение минимумов и максимумов в питоне без списков - PullRequest
0 голосов
/ 12 октября 2018

Поэтому мне нужно написать код, чтобы найти минимальное и максимальное число из набора целых чисел, введенных пользователем, но я не могу использовать списки.Вот так выглядит мой код.

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, который я мог бы использовать, или что-то из математического модуля, возможно?

1 Ответ

0 голосов
/ 12 октября 2018

Чтобы исправить ваш код: Вы можете установить 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
...