![](/img/trans.png)
[英]How do I find the smallest, largest value and total and the average in a 2D list that the user provides in python?
[英]How do i find the smallest or largest value in a list in Python
我是编程新手。 因此,假设我想使用年龄 python 中的列表连续存储一个值,如果我发现某人是最年轻/最年长的,我想在屏幕上说出来。 我试过了,但它似乎不起作用,谁能告诉我出了什么问题并帮助我?
ageslst= []
while True:
age = int(input('age?'))
ageslst.append(agelst)
if age > max(ages):
print('Oldest')
if age < min (agelst):
print(' Youngest')
这将执行您要执行的操作:
ageslst= []
while True:
age = int(input('age?'))
ageslst.append(age)
if age == max(ageslst):
print('Oldest')
if age == min(ageslst):
print('Youngest')
我修复了你的第二个if
语句的缩进,调整了你的变量以在它们应该使用的地方实际使用,我还将测试条件从>
和<
更改为==
(测试相等性 – =
是赋值运算符)。 如果用户输入到目前为止最大的年龄,它会被添加到ageslst
并且现在是那里的最大值。 因此测试if age > max(ageslst)
永远不会为 True。
最后,您可能应该向循环添加某种终止条件,否则它将永远运行。
这里有几个问题:
ageslst.append(age)
if age > max(ages):
print('Oldest')
if age < min (ages):
print(' Youngest')
每个新年龄都存储在age
变量中, ageslst
是您累积的所有年龄的列表。 您想要做的是将新年龄与所有先前年龄的列表进行比较。
接下来,如果您在检查之前将age
附加到列表中,那么您的if
条件将永远不会为True
,因为新的年龄始终已在列表中。
对其进行修改以解决这些问题:
if age > max(ageslst):
print('Oldest')
elif age < min (ageslst):
print(' Youngest')
ageslst.append(age)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.