定义一个 compare 函数和一个 max_value 函数,compare 函数用于比较两个数的大小,max_value 函数用于得到一串整型数据中的最大值。用python
时间: 2023-12-14 18:39:23 浏览: 59
Python中用max()方法求最大值的介绍
以下是 compare 函数的实现:
```python
def compare(num1, num2):
if num1 > num2:
return "num1 is greater than num2"
elif num1 < num2:
return "num2 is greater than num1"
else:
return "num1 and num2 are equal"
```
以下是 max_value 函数的实现:
```python
def max_value(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
```
使用示例:
```python
print(compare(5, 10)) # 输出 "num2 is greater than num1"
print(compare(7, 7)) # 输出 "num1 and num2 are equal"
numbers = [3, 8, 1, 9, 4]
print(max_value(numbers)) # 输出 9
```
阅读全文