我对编程非常陌生,主要是为了减轻工作压力,所以我刚开始尝试使用python,并且试图找到X数量范围。
该程序可以运行,但不是使用列表的最后2个值来返回最小值和最大值。
正如我所说,我只是出于业余爱好而做,并不具备丰富的经验,但是毫无疑问,这很简单,但是却很难理解为什么它没有采用应有的价值观。 / p>
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)
在此先感谢您的帮助
答案 0 :(得分: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)
答案 1 :(得分:0)
它应该工作,我在repl.it上对其进行了测试,它的min max max很好。您可能希望将item
设置为一个整数,从而使后续的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)