Hi Peter,
On 22 November 2012 12:55, Peter O'Doherty <m...@peterodoherty.net> wrote: > Hi list, > Firstly, apologies for the low-level nature of this question - it's really > quite basic but I don't seem to be able to solve it. > > I need to write a program that examines 3 variables x, y, z, and prints > the largest odd number. I've tried all sorts of variations and this is the > current version: > > x, y, z = 26, 15, 20 > > if x > y and x > z and x%2 != 0: > print 'x is largest and odd' > elif y > x and y > z and y%2 != 0: > print 'y is largest and odd' > elif z > x and z > y and z%2 != 0: > print 'z is largest and odd' > else: > print 'no odd' > The key logical mistake you make is that by your current logic the *smallest* number can never be the largest odd number, which is obviously false as in your example. Break the problem down (divide and conquer). Suppose I gave you only 2 numbers, and you had to say which of the 2 numbers were the largest odd, what would be the possible outcomes and what would the the solution be? (Hint, both can be odd, only x can be odd, only y can be odd, or neither can be odd.) Once you have that answer, then repeat the exact same solution for the first 2 numbers and apply to the answer from x&y and and the remaining z. The result from that is tells you the largest odd number from all 3. (Aside, your question states to print the largest odd number, which I interpret to mean the value, not the name of the variable holding the value. ) Walter
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor