|
| 1 | +''' |
| 2 | +Exercise 28: Max Of Three |
| 3 | + |
| 4 | +Implement a function that takes as input three variables, and |
| 5 | +returns the largest of the three. Do this without using the Python |
| 6 | +max() function! |
| 7 | + |
| 8 | +The goal of this exercise is to think about some internals that |
| 9 | +Python normally takes care of for us. All you need is some |
| 10 | +variables and if statements! |
| 11 | + |
| 12 | +''' |
| 13 | + |
| 14 | +# Solution |
| 15 | +def get_input(): |
| 16 | + """ |
| 17 | + Get the user input number, split by space. |
| 18 | + |
| 19 | + Returns: |
| 20 | + input_number -- a list contain all the numbers of user's input |
| 21 | + """ |
| 22 | + input_number = input('Please type some numbers, split by space:').split() |
| 23 | + try: |
| 24 | + input_number = [float(i) for i in input_number] |
| 25 | + except ValueError: |
| 26 | + print('Input error') |
| 27 | + input_number = [] |
| 28 | + return input_number |
| 29 | + |
| 30 | +def get_max(input_number): |
| 31 | + """ |
| 32 | + Get the max value in input_numebr list. |
| 33 | + |
| 34 | + Arguments: |
| 35 | + input_number -- a list contain all the numbers of user's input |
| 36 | + |
| 37 | + Returns: |
| 38 | + max_value -- the max value in input_numebr list. |
| 39 | + """ |
| 40 | + max = float('-inf') |
| 41 | + for i in input_number: |
| 42 | + max = i if i > max else max |
| 43 | + if len(input_number) == 0: |
| 44 | + max = 'Please check your input' |
| 45 | + return max |
| 46 | + |
| 47 | +def main(): |
| 48 | + input_number = get_input() |
| 49 | + print(get_max(input_number)) |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments