我最近开始学习如何使用python编写代码。 在这段代码中,我想找到最大和第二大输入数字(大学教员的年龄)。 但是它不起作用! 我不知道问题出在哪里! 提前谢谢
oldest = 0
oldest2 = 0
for i in range (100):
age = int(input())
if age > oldest :
oldest = age
age = int(input())
if age > oldest2 and age < oldest:
oldest2 = age
if age == -1:
break
print (oldest, oldest2)
以下几点应该行得通。这只是你已经拥有的东西的一个微小变化。您是在寻找实现结果的最佳方式,还是有特定的要求
以下为试验数据
结果是
每个循环会得到两次年龄,并且每个循环只考虑两个选项中的一个。你最多只能说对了一半。相反,让python为您完成这项工作。您可以将年龄收集到一个列表中,进行排序,然后提取数字
相关问题 更多 >
编程相关推荐