Список Python неправильно выбирает минимальное и максимальное значения - PullRequest
0 голосов
/ 03 июля 2018

Я очень новичок в программировании и занимаюсь этим в основном как хобби, чтобы снять стресс с работы, я только начал пробовать себя на python и пытаюсь найти диапазон количества X чисел.

программа работает, но вместо возвращаемых минимальных и максимальных значений она использует последние 2 значения из списка.

Как я уже сказал, я делаю это как хобби и не обладаю огромным опытом, но я не сомневаюсь, что это что-то простое, но я изо всех сил пытаюсь понять, почему он не принимает значения, которые он должен.

numSales = int(input("How many numbers are you entering? "))
# creates a variable for number of numbers, allowing the list to be of length determined by the user

noNumbers = []
# creates list called noNumbers

maxListLength = numSales
# gives the maximum length of the list called dailySales
while len(noNumbers) < maxListLength:
    item = input("Enter a number to add to the list: ")
    noNumbers.append(item)

# Asks for an input as long as the number of variables in dailySales is less than the value of maxListLength
print(noNumbers)

lowest = int(min(noNumbers))
# returns the lowest value of dailySales
print(lowest)

highest = int(max(noNumbers))
# returns the largest value of dailySales
print(highest)

numRange = int(highest - lowest)
# need to subtract highest from lowest to print the sales range

print(numRange)

Заранее спасибо за помощь

Ответы [ 2 ]

0 голосов
/ 03 июля 2018

Вам нужно вставить в список целые вместо строк, просто измените:

item = input("Enter a number to add to the list: ")
noNumbers.append(item)

Автор:

item = int(input("Enter a number to add to the list: "))
noNumbers.append(item)
0 голосов
/ 03 июля 2018

Это должно работать, я проверил его на repl.it, и он сделал min max просто отлично. Возможно, вы захотите сделать item int, так как последующие int() вызовы станут ненужными:

https://repl.it/repls/ChubbyAlienatedModulus

numSales = int(input("How many numbers are you entering? "))
# creates a variable for number of numbers, allowing the list to be of length determined by the user

noNumbers = []
# creates list called noNumbers

maxListLength = numSales
# gives the maximum length of the list called dailySales
while len(noNumbers) < maxListLength:
    item = int(input("Enter a number to add to the list: "))
    noNumbers.append(item)

# Asks for an input as long as the number of variables in dailySales is less than the value of maxListLength
print(noNumbers)

lowest = min(noNumbers)
# returns the lowest value of dailySales
print(lowest)

highest = max(noNumbers)
# returns the largest value of dailySales
print(highest)

numRange = highest - lowest
# need to subtract highest from lowest to print the sales range

print(numRange)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...