python编写一个函数,该函数能比较三个数的大小并调用
时间: 2024-10-23 20:02:21 浏览: 14
python怎么调用自己的函数
在Python中,你可以编写一个名为`compare_numbers`的函数,用于比较三个数的大小。这里是一个简单的实现示例:
```python
def compare_numbers(num1, num2, num3):
if num1 > num2 and num1 > num3:
return f"{num1} is the largest."
elif num2 > num1 and num2 > num3:
return f"{num2} is the largest."
else:
return f"{num3} is the largest."
# 测试函数
print(compare_numbers(5, 3, 7)) # 输出 "7 is the largest."
print(compare_numbers(1, 9, 4)) # 输出 "9 is the largest."
print(compare_numbers(0, -2, 0)) # 输出 "0 is the largest." (因为相等视为最小)
阅读全文