[Tutor] Beginner's question
Peter O'Doherty
mail at peterodoherty.net
Thu Nov 22 14:14:08 CET 2012
Hi Varun,
Thanks for your reply.
I agree the problem is logic - but how can one inspect one number using
if x%2 == 0 and then compare it to two other numbers which should at the
same time be checked for "oddness", just using the basic constructs?
Thanks,
Peter
On 11/22/2012 02:06 PM, Varun Nagpal wrote:
> Hi Peter,
>
> The reason why your program is not behaving as it should is not
> Pythonic but rather logic.
>
> Instead of giving away the code, I would suggest you revisit the 'and'
> conditions.
>
> The program should print the largest odd number i.e. the number which
> is largest among all odd numbers.
> Instead you are printing the number which is largest AND odd.
>
> --
> Varun
>
>
> On Thu, Nov 22, 2012 at 6:25 PM, Peter O'Doherty
> <mail at peterodoherty.net <mailto:mail at 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'
>
>
> A solution should be possible using only the simple operators and
> keywords above, no functions, lists or any other form of
> iteration. (It's from p. 16 of Introduction to Computation and
> Programming Using Python, and no, it's not "homework"!)
>
> Many thanks,
> Peter
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
//=============================
-> Peter O'Doherty
-> http://www.peterodoherty.net
-> mail at peterodoherty.net
-> https://joindiaspora.com/people/70716
//=============================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121122/b535dddf/attachment.html>
More information about the Tutor
mailing list