Python 我不能得到一个元组的最小值或最大值

Python 我不能得到一个元组的最小值或最大值,python,Python,这就是它的输出 total_price = [] for i in range (5): try: price = list (input ("Enter the price of the sweet: ")) except ValueError: print("Enter an integer") total_price.append(price) print (total_price) print ("The most expensive sweet is " + st

这就是它的输出

total_price = []

for i in range (5):
try:
     price = list (input ("Enter the price of the sweet: "))
except ValueError:
    print("Enter an integer")
total_price.append(price)
print (total_price)

print ("The most expensive sweet is " + str (max(total_price)))
print ("The least expensive sweet is" + str (min(total_price)))

我已经成功地达到了那个阶段,但由于某种原因,我仍然遇到问题,因为它正在分离数组中的值。

Python告诉您这个问题,总价是不可接受的,因为它是一个整数,就像问max(7)是什么。您可能需要将所有5个输入存储在某个数组中,然后在该数组上调用sum、max、min

范例--


您使用的是
max
函数,该函数需要一个iterable或两个以上的参数来选择较高的值,如其定义所示:

返回iterable中的最大项或两个或多个参数中的最大项“


但是您传递的是一个整数

如果您将价格存储在一个列表中,该列表是可编辑的,那么您可以找到最昂贵的糖果的价格。例如:

A = [1, 2, 3, 4, 5]
print(max(A)) # 5
print(min(A)) # 1
print(sum(A)) # 15

要查找元组中的最大值,如标题所示,请尝试
max(列表(价格))

在这里,您可以将
str
应用于
list
。例如

# omitted part
try:
    price = list(input("Enter the price of the sweet: "))
except ValueError:
    print("Enter an integer")
要解决此问题,请使用
int

read_value = '1234'
list(read_value)
Out:
['1', '2', '3', '4']  # type: list of str
对于单输入

# omitted part
try:
    price = int(input("Enter the price of the sweet: "))
except ValueError:
    print("Enter an integer")

您使用的是哪种代码?请始终将相关代码和完整的错误回溯包含在文本中,而不是图像中。我在Python中开发。您应该将代码作为文本而不是图像发布。如果找到解决方案,请将其作为答案发布,而不是在您的问题中。谢谢。如果我将其存储在数组中,我将如何执行此操作?
prices=[]prices.append(some_value)
我尝试过这个方法,但由于某些原因,它会将数组中的值分隔开。我已经用我目前得到的内容更新了我的原始帖子。你能添加一些示例来改进你的答案吗?
read_value = '1234'
list(read_value)
Out:
['1', '2', '3', '4']  # type: list of str
# omitted part
try:
    price = int(input("Enter the price of the sweet: "))
except ValueError:
    print("Enter an integer")
read_value = '1234'
int(read_value)
Out:
1234  # type: int