|
| 1 | +# exercise 7.2.1 from unit 7 |
| 2 | +''' |
| 3 | +Write a function called is_greater defined as follows: |
| 4 | +
|
| 5 | +def is_greater(my_list, n): |
| 6 | +The function accepts two parameters: a list and a number n. |
| 7 | +The function returns a new list containing all the elements that are greater than the number n. |
| 8 | +
|
| 9 | +An example of running the is_greater function: |
| 10 | +>>> result = is_greater([1, 30, 25, 60, 27, 28], 28) |
| 11 | +>>> print(result) |
| 12 | +[30, 60] |
| 13 | +''' |
| 14 | + |
| 15 | +def is_greater(my_list, n): |
| 16 | + result = [] |
| 17 | + for element in my_list: |
| 18 | + if element > n: |
| 19 | + result.append(element) |
| 20 | + return result |
| 21 | + |
| 22 | +def main(): |
| 23 | + result = is_greater([1, 30, 25, 60, 27, 28], 28) |
| 24 | + print(result) # print [30, 60] |
| 25 | + result = is_greater([1, 2, 3, 4, 5, 6], 3) |
| 26 | + print(result) # print [4, 5, 6] |
| 27 | + result = is_greater([10, 20, 30, 40], 15) |
| 28 | + print(result) # print [20, 30, 40] |
| 29 | + result = is_greater([1, 2, 3, 4, 5, 6], 6) |
| 30 | + print(result) # print [] |
| 31 | + |
| 32 | +if __name__ == "__main__": |
| 33 | + main() |
0 commit comments