Python列表未正确拾取最小值和最大值

Python列表未正确拾取最小值和最大值,python,list,Python,List,我对编程非常陌生,主要是作为一种业余爱好来减轻工作压力,我刚刚开始尝试python,我正试图找到X个数字的范围 该程序可以工作,但它使用列表的最后2个值,而不是返回最小值和最大值 正如我所说,我做这件事是一种爱好,没有太多的经验,但我毫不怀疑这件事很简单,但我很难理解为什么它没有采取应有的价值观 numSales = int(input("How many numbers are you entering? ")) # creates a variable for number of numbe

我对编程非常陌生,主要是作为一种业余爱好来减轻工作压力,我刚刚开始尝试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)

提前感谢您的帮助

它应该可以工作,我在一个repl.It上测试了它,它的最小最大值非常好。您可能希望将该项设置为int,这样就不需要后续的int调用:


您需要在列表中插入整数而不是字符串,只需修改:

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


对min和max的调用是比较字符串,而不是数字。在列表中存储数字:noNumbers.appendintitem。@chepner有正确的答案,但也值得一提,今后请将您的评论置于您的声明之上,而不是之下。欢迎使用StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。@Nearoo您不断重复此断言,但您是不正确的,字符串编号是按词汇排序的,而不是按数字排序的,即max'2',10'='2'@wwii是的,但太长了:wiki说现在不需要后续的int调用了!哦,好吧,我应该把它去掉吗?这也是不正确的。Python按照您直观期望的方式对字符串数字进行排序。@SheshankS。让我重新措辞。最小值和最大值返回预期值,即使所有值都是str。请自己尝试。错误。Python按照您直观的预期方式对字符串数字进行排序。这应该没什么区别。10<2在python中等于True。@Nearoo您不正确,词法排序与数字排序不同。@GabrielBenCompte您说得对,奇怪!但请在包含字符串的列表中尝试“最小值”和“最大值”。它将吐出正确的值。另一个时间min[10,2,7,4]=10。排序时,除非第一个数相等,否则只比较第一个数。这就是为什么10000000小于2,因为1
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)