forked from me701/python_example_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_largest.py
More file actions
30 lines (26 loc) · 766 Bytes
/
Copy pathfirst_largest.py
File metadata and controls
30 lines (26 loc) · 766 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
28
29
30
"""
Python program to find the largest element and its location.
"""
def largest_element(a, loc = False):
""" Return the largest element of a sequence a.
"""
try:
maxval = a[0]
location = 0
for (i,e) in enumerate(a):
if e > maxval:
maxval = e
location = i
if loc == True:
return maxval, location
else:
return maxval
except ValueError:
return "Value Error"
except TypeError:
return "Type error, my dude. You can't compare those."
except:
return "Unforseen error! What did you do?"
if __name__ == "__main__":
a = ["a","b","c",2,1]
print("Largest element is {:}".format(largest_element(a, loc=True)))