Choose largest odd number python

python

Solution

it would be better to filter the numbers, then sort them.

numbers = [x, y, z]

sorted_odd_nums = sorted((x for x in enumerate(numbers) if x[1]%2), 
                         key = lambda x:x[1], 
                         reverse=True)

if not sorted_odd_nums:
   # all numbers were even and filtered out.
elif sorted_odd_nums[0][0] == 0:
   # x is the biggest odd number
elif sorted_odd_nums[0][0] == 1:
   # y is the biggest odd number
elif sorted_odd_nums[0][0] == 2:
   # z is the biggest odd number

what it does:

`enumerate(numbers)` returns a sequence of `(index, item)` pairs. since original list was `[x, y, z]`, we can keep the track of `x`, `y`, `z` even after filter and sort.

`(x for x in enumerate(numbers) if x[1]%2)` filters above enumeration if the second item in given tuple is not an even number.

`sort( ... , key=lambda x:x[1], reverse=True)` sorts filtered items using value of their second-indexed item(which is original number) in descending order.

user input

to read from user, the easiest way is using `raw_input`(py2) / `input`(py3k).

number = int(raw_input('enter a number: '))

using only if-statements

you'd have to nest if-statements. like:

if x%2: # x is odd
  if y%2: # y is odd
    if z%2: #z is odd
      if x>y and x>z: #x is the biggest odd number
      elif y>z and y>x: #y is the biggest odd number
      elif z>x and z>y: #z is the biggest odd number

    else: #z is even
      if x>y: #x is the biggest odd number
      else: #y is the biggest odd number
  else: #y is even
    if z%2: #z is odd
...

Problem

I am trying to make a simple program in Python that calculates the largest odd number out of the values x, y, z. How do I give the user an option to pick the values for x, y, and z? So the program will ask what x, y, and z is and then say "x,y,z is the largest odd" or the numbers are all even. What I have so far is below. Is this at least a decent start? ``` # This program exmamines variables x, y, and z # and prints the largest odd number among them if x%2 !== 0 and x > y and y > z: print 'x is the largest odd among x, y, and z' elif y%2 !== 0 and y > z and z > x: print 'y is the largest odd among x, y, and z' elif z%2 !== 0 and z > y and y > x: print 'z is the largest odd among x, y, and z' elif x%2 == 0 or y%2 == 0 or z%2 == 0: print 'even' ``` With thkang post, I now have: ``` # This program exmamines variables x, y, and z # and prints the largest odd number among them if x%2 !== 0: if y%2 !== 0: if z%2 !== 0: if x > y and x > z: #x is the biggest odd elif y > z and y > x: #y is the biggest odd elif z > x and z > y: #z is the biggest odd else: #z is even if x > y: #x is the biggest odd else: #y is the biggest odd else: #y is even if z%2 != 0: #z is odd if x > z: #x is the biggest odd else: #z is the biggest odd else: #y,z are even and x is the biggest odd else: #x is even if y%2 != 0 and z%2 != 0; #y,z is odd if y > z: #y is the biggest odd else: #z is the biggest odd else: #x and y is even if z%2 != 0: #z is the biggest odd ```

Original source