Python列表未正确选择最小值和最大值

2024-09-28 03:15:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

提前谢谢你的帮助


Tags: ofthetonumberinputvaluelistint
2条回答

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

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)

应该能用的,我在一台电脑上测试过复制它做得很好。您可能希望将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)

相关问题 更多 >

    热门问题