Define the function select_numbers(list1) that stores numbers from a list using loop that satisfy the following conditions in a list and returns that list. The number must be divisible by five If the number is greater than 150, then skip it and move to the next number If the number is greater than 500, then stop the loop Example: list1 = [12, 75, 150, 180, 145, 525, 50] Expected Output: [75, 150, 145]
时间: 2023-07-16 08:13:23 浏览: 106
Here's the implementation of the select_numbers() function in Python:
```python
def select_numbers(list1):
result = []
for num in list1:
if num % 5 == 0 and num > 0 and num <= 150:
result.append(num)
elif num > 150 and num < 500:
continue
elif num >= 500:
break
return result
```
The function takes a list of numbers as input and initializes an empty list called 'result'. It then loops through each number in the input list 'list1' and checks if it satisfies the given conditions. If a number is divisible by 5 and greater than 0 but less than or equal to 150, it is added to the 'result' list using the 'append' method. If a number is greater than 150 but less than 500, it is skipped using the 'continue' statement. If a number is greater than or equal to 500, the loop is stopped using the 'break' statement. Finally, the 'result' list is returned.
For example, if we call the function with the input list `list1 = [12, 75, 150, 180, 145, 525, 50]`, the output will be `[75, 150, 145]`.
阅读全文