You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python代码以输出最高海拔山峰的名称与高度?

How to Get the Highest Mountain with Full Details in Python

Hey, I see your current code only outputs the highest elevation number, but you want to display both the mountain name and its height in the format "Highest is: Mount Everest 8850". Let's fix that easily!

The Problem with Your Current Code

Your existing max() call only passes individual height values, so it just returns the largest number instead of the full mountain object that holds both name and height info.

The Modified Solution

Instead of feeding separate height values to max(), we'll pass a list of all your Mountain instances, and tell max() to use the height attribute as the comparison key. This way, it returns the actual mountain object, and we can access both its name and height properties to build your desired output.

class Mountain: 
    def __init__(self, name, height): 
        self.name = name 
        self.height = height 

# Create all mountain instances
mountain_01 = Mountain("Mount Everest", 8850) 
mountain_02 = Mountain("Mount Kazbek", 5033) 
mountain_03 = Mountain("Vinson Massif", 4892) 
mountain_04 = Mountain("Kilimanjaro", 5895) 

# Get the mountain object with the highest height
highest_mountain = max([mountain_01, mountain_02, mountain_03, mountain_04], key=lambda x: x.height) 

# Print the desired format
print(f"Highest is: {highest_mountain.name} {highest_mountain.height}")

What Changed?

  • We wrapped all mountain instances into a list: [mountain_01, mountain_02, mountain_03, mountain_04]
  • Added the key parameter to max(): key=lambda x: x.height tells the function to compare each mountain by its height attribute
  • Now highest_mountain is a full Mountain object, so we can access highest_mountain.name and highest_mountain.height to output exactly the format you want

内容的提问来源于stack exchange,提问作者Sandro Kakoishvili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:12:18