输入10个整数,将其中最小的数与第一个数互换,把最大的数与最后一个互换。写3个函数,(1)输入10个数;(2)进行处理;(3)输出10个数。
时间: 2023-12-04 17:40:41 浏览: 19
以下是实现题目要求的三个函数:
1. 输入10个数
```python
def input_numbers():
numbers = []
for i in range(10):
number = int(input("请输入第{}个整数:".format(i+1)))
numbers.append(number)
return numbers
```
2. 进行处理
```python
def process_numbers(numbers):
min_index = numbers.index(min(numbers))
max_index = numbers.index(max(numbers))
numbers[0], numbers[min_index] = numbers[min_index], numbers[0]
numbers[-1], numbers[max_index] = numbers[max_index], numbers[-1]
return numbers
```
3. 输出10个数
```python
def output_numbers(numbers):
for number in numbers:
print(number, end=' ')
```
然后,我们可以将这三个函数组合起来,实现完整的程序:
```python
def input_numbers():
numbers = []
for i in range(10):
number = int(input("请输入第{}个整数:".format(i+1)))
numbers.append(number)
return numbers
def process_numbers(numbers):
min_index = numbers.index(min(numbers))
max_index = numbers.index(max(numbers))
numbers[0], numbers[min_index] = numbers[min_index], numbers[0]
numbers[-1], numbers[max_index] = numbers[max_index], numbers[-1]
return numbers
def output_numbers(numbers):
for number in numbers:
print(number, end=' ')
if __name__ == '__main__':
numbers = input_numbers()
numbers = process_numbers(numbers)
output_numbers(numbers)
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)