让用户输入三个整数,要求打印输出最大和最小的数字
时间: 2024-04-18 15:29:55 浏览: 11
下一个数。
以下是代码示例:
```
def input_numbers():
numbers = []
for i in range(5):
num = int(input(f"请输入第{i+1}个整数: "))
numbers.append(num)
return numbers
def swap_min_first(numbers):
min_index = numbers.index(min(numbers))
numbers[0], numbers[min_index] = numbers[min_index], numbers[0]
def swap_max_last(numbers):
max_index = numbers.index(max(numbers))
numbers[-1], numbers[max_index] = numbers[max_index], numbers[-1]
if __name__ == '__main__':
nums = input_numbers()
print("原始数字列表:", nums)
swap_min_first(nums)
print("最小数字与第一个数字对换后的列表:", nums)
swap_max_last(nums)
print("最大数字与最后一个数字对换后的列表:", nums)
```
运行示例:
```
请输入第1个整数: 5
请输入第2个整数: 2
请输入第3个整数: 7
请输入第4个整数: 1
请输入第5个整数: 9
原始数字列表: [5, 2, 7, 1, 9]
最小数字与第一个数字对换后的列表: [1, 2, 7, 5, 9]
最大数字与最后一个数字对换后的列表: [1, 2, 9, 5, 7]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)