-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmaxminlist.py
More file actions
27 lines (22 loc) · 820 Bytes
/
maxminlist.py
File metadata and controls
27 lines (22 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def find_max_min(input_list):
if not input_list:
return None, None # Return None for both max and min if the list is empty
# Initialize max and min with the first element of the list
max_value = input_list[0]
min_value = input_list[0]
# Traverse the list to find the maximum and minimum values
for num in input_list:
if num > max_value:
max_value = num
elif num < min_value:
min_value = num
return max_value, min_value
# Example usage:
if __name__ == "__main__":
numbers = [12, 45, 67, 89, 34, 23, 9]
max_num, min_num = find_max_min(numbers)
if max_num is not None and min_num is not None:
print(f"Maximum value: {max_num}")
print(f"Minimum value: {min_num}")
else:
print("The list is empty.")