Good evening. I tried to write a code for solving the problem stated by Mr. Guttang on his book, which is the following: Write a program that examines three variables - x, y and z - and prints the largest odd number among them. If none of them are odd, it should print a message to that effect. The code I wrote is the following (in the comments):
``` x=float(raw_input('Enter 1st number: ')) y=float(raw_input('Enter 2nd number: ')) z=float(raw_input('Enter 3rd number: ')) if x%2!=0.0: if y%2==0.0 and z%2==0.0: print x, ' is the only odd number' elif (y%2!=0.0 or y%2==0.0) and (z%2!=0.0 or z%2==0.0) and (x>y and x>z): print x, ' is the biggest' elif y%2!=0.0: if x%2==0.0 and z%2==0.0: print y, ' is the only odd number' elif (x%2!=0.0 or x%2==0.0) and (z%2!=0.0 or z%2==0.0) and (y>x and y>z): print y, ' is the biggest' if z%2!=0.0: if y%2==0.0 and x%2==0.0: print z, ' is the only odd number' elif (y%2!=0.0 or y%2==0.0) and (x%2!=0.0 or x%2==0.0) and (z>y and z>x): print x, ' is the biggest' else: if x%2==0.0 and y%2==0.0 and z%2==0.0: print 'None of them is an odd number' ```
Well, I've tested it with x = 3, y = 5 and z = 7. It prints that 3 is the biggest odd number. x=3 is true for the first condition (x%2!=0.0). y = 5 and z = 7 are false for the second condition, so the program continues computing. Those given values are true for the first elif, but are false for the last condition is this elif statement: x>y and x>z. Since TRUE and TRUE and FALSE = FALSE, I wasn't expecting it to print x. Does anyone know what happened? Furthermore, does anyone know how to solve it in another way? Thanks a lot.
Is this python?
So I take it what your look for here is code that checks three odd numbers with each other to see if they are odd, if not it prints "no odd numbers" Here is what I came up with.
x=float(raw_input('Enter 1st number: ')) y=float(raw_input('Enter 2nd number: ')) z=float(raw_input('Enter 3rd number: ')) number = 0; if x%2 != 0 or y%2 != 0 or z%2 != 0: if x%2 != 0 and y%2 != 0: if x > y and x > number: number = y elif y < number: number = x if x%2 != 0 and z%2 != 0: if x > z and x > number: number = x elif z > number: number = z if y%2 != 0 and z%2 != 0: if y > z and y > number: number = y elif z > number: number = z if x%2 == 0 and y%2 == 0 and z%2 == 0: print 'No odd numbers' else: print number, 'is the biggest.'
Well thank you :)
np
What seems to be the problem?
Does the code do what you need?
I reckon so. Well, I've got a question for you. In the first block of your code, the IF condition, you wrote ``` if x%2 != 0 or y%2 != 0 or z%2 != 0: if x%2 != 0 and y%2 != 0: if x > y and x > number: number = y elif y < number: number = x ``` I did not understand why you assigned y to number if the ELIF statement. May I ask you a piece of explanation about it?
I meant "you assigned x to number", if y<number
Hmm, I think I might have made a mistake. it should be: if x%2 != 0 or y%2 != 0 or z%2 != 0: if x%2 != 0 and y%2 != 0: if x > y and x > number: number = x elif y > number: number = y
Ok, now it makes more sense. Let me check it please.
Sure, sorry about that, glad you caught it.
In the first If block, it evaluates X and Y. In the second one, X and Z. In the third one, Y and Z. So, it stores a temporary value on variable "number" and then compares it in the next block, right? I mean, it's not about boolean evaluation, it's storing and comparing the results, right?
Exactly, you've got it. :)
Well done, man! Or girl, I don't know. Anyway thanks again :D
Your welcome
:)
Join our real-time social learning platform and learn together with your friends!