python编程 求输入的10个数中的最大值和最小值,并输出它们各自是第几个
时间: 2023-07-29 08:11:40 浏览: 93
好的,可以用以下代码实现:
```
nums = []
for i in range(10):
num = int(input(f"请输入第{i+1}个数:"))
nums.append(num)
max_num = max(nums)
min_num = min(nums)
max_index = nums.index(max_num) + 1
min_index = nums.index(min_num) + 1
print(f"最大值为{max_num},是第{max_index}个数")
print(f"最小值为{min_num},是第{min_index}个数")
```
解释一下:
1. 首先定义一个空列表 `nums`,用于存储输入的10个数。
2. 使用 `for` 循环输入10个数,每次输入后将其添加到 `nums` 列表中。
3. 使用 `max` 和 `min` 函数分别找出 `nums` 列表中的最大值和最小值,并将其分别赋值给 `max_num` 和 `min_num` 变量。
4. 使用 `index` 方法找出最大值和最小值在 `nums` 列表中的索引,由于索引从0开始,因此需要加1,将其分别赋值给 `max_index` 和 `min_index` 变量。
5. 最后使用 `print` 函数输出结果。
相关问题
写几个python编程题
1. 编写一个函数,输入两个字符串,判断第一个字符串是否是第二个字符串的子串。
```python
def is_substring(s1, s2):
return s1 in s2
```
2. 编写一个函数,输入一个整数列表,返回其中的最大值和最小值。
```python
def find_min_max(nums):
if not nums:
return None, None
min_val, max_val = nums[0], nums[0]
for num in nums:
if num < min_val:
min_val = num
if num > max_val:
max_val = num
return min_val, max_val
```
3. 编写一个函数,输入一个字符串,返回该字符串中每个字符出现的次数。
```python
def count_chars(s):
counts = {}
for char in s:
counts[char] = counts.get(char, 0) + 1
return counts
```
4. 编写一个函数,输入一个整数n,返回斐波那契数列的前n项。
```python
def fibonacci(n):
if n < 1:
return []
if n == 1:
return [0]
fibs = [0, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs
```
5. 编写一个函数,输入一个字符串,判断是否是回文字符串。
```python
def is_palindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
```
编程实现:物业管理信息系统中 对物业公司工作人员 的绩效考核分数统计功能。 运行窗口中,提示用户依次输入员工工号(三位数,001、002、003......)和某月的绩效考核分数(百分制),直到输入最后一位员工的为止,输出 所有员工分数的平均值、其中的最大值、最小值、及得60分以下的有几位员工(是哪几位(工号))。
好的,这是一个比较基础的小型项目,使用 Python 编程语言可以实现,以下是代码实现:
```python
# 定义一个空字典用于存储员工工号和绩效考核分数
employee_scores = {}
# 循环输入员工工号和绩效考核分数
while True:
employee_id = input("请输入员工工号(三位数,输入0结束):")
if employee_id == "0":
break
score = int(input("请输入该员工本月的绩效考核分数(百分制):"))
employee_scores[employee_id] = score
# 计算平均值、最大值、最小值和得60分以下的员工数和工号
total_score = 0
max_score = 0
min_score = 100
below_60_count = 0
below_60_employee_ids = []
for employee_id, score in employee_scores.items():
total_score += score
if score > max_score:
max_score = score
if score < min_score:
min_score = score
if score < 60:
below_60_count += 1
below_60_employee_ids.append(employee_id)
average_score = total_score / len(employee_scores)
# 输出结果
print("所有员工分数的平均值为:", average_score)
print("其中的最大值为:", max_score)
print("其中的最小值为:", min_score)
print("得60分以下的有", below_60_count, "位员工,他们的工号分别为:", below_60_employee_ids)
```
当你运行这段代码时,会依次提示你输入员工工号和绩效考核分数,直到你输入 0 为止。然后程序会计算出所有员工分数的平均值、最大值、最小值和得60分以下的员工数和工号,并输出结果。