Your solution is probably a bit too verbose and specific. What if you needed to find the largest odd number and there were four? Or a hundred?
A better idea would be to use Python's max function to examine a sequence that filters out even numbers.
You can collect the numbers to be examined in a sequence like a list:
A = [53, 31, 59, 75, 25, 32, 99, 15, 63, 35]
Then you can filter that sequence with a list comprehension:
A_odd = [n for n in A if n % 2 != 0]
Then you can find the max of that sequence:
max_odd = max(A_odd)
Suppose that there were no odd numbers in the sequence, though. The max
function raises an exception if its argument is empty. So we can use a conditional expression to check to see if A_odd
is empty:
if A_odd:
max_odd = max(A_odd)
else:
max_odd = "No odd numbers in sequence"
Then when we print the answer with print(A_odd)
we get the result or the message.
Python 3.4 (not yet released) includes an addition to the max function that allows this to be even more compact - you can skip the conditional and just do max(A_odd, default="No odd numbers in sequence")