
It would be better to filter the numbers, then sort them. I appreciate it has been aswered, but by providing this answer, other users are more likely to come across it when searching for answers to the question in the book. It seems to work for all the combinations I have tried, but it would be useful to know if it can be shortened bearing in mind my points from paragraph three. To that end this is the code I wrote: if x%2 = 0: If none of them are odd, it should print a message to that effect.'Īt this point the book has only introduced variable assignment and conditional branching programs and the print function. 'Write a program that examines three variables x,y and z, and prints the largest odd number among them. The book is written to be used with Python 2.7. It's the recommended text for the MITx: 6.00x Introduction to Computer Science and Programming. This is the finger exercise from Ch.2 of Introduction to Computation and Programming Using Python by John V. ValueError: max() arg is an empty sequence If you pass an empty sequence or provide only even numbers, you will get a ValueError: > find_largest_odd(0) return max(filter(lambda x: x & 1, args)) return max(arg for arg in args if arg & 1) Return max(filter(lambda x: x & 1, args)) Return max(arg for arg in args if arg & 1)

Using builtins like this is safer/more reliable because it is simpler to compose them, the code is well-tested, and the code executes mostly in C (rather than multiple byte code instructions). Use either generator or filter to find only the odd numbers. If x > y and x > z: #x is the biggest oddĮlif y > z and y > x: #y is the biggest oddĮlif z > x and z > y: #z is the biggest oddĮlse: #y,z are even and x is the biggest oddĪvoid using if-stmts to find maximum. With thkang post, I now have: # This program exmamines variables x, y, and z Print 'z is the largest odd among x, y, and z'

Print 'y is the largest odd among x, y, and z' Print 'x is the largest odd among x, y, and z' # and prints the largest odd number among them Is this at least a decent start? # This program exmamines variables 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. How do I give the user an option to pick the values for x, y, and z? I am trying to make a simple program in Python that calculates the largest odd number out of the values x, y, z.
