Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

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):

OpenStudy (anonymous):

``` 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' ```

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

Is this python?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.'

OpenStudy (anonymous):

Well thank you :)

OpenStudy (anonymous):

np

OpenStudy (anonymous):

What seems to be the problem?

OpenStudy (anonymous):

Does the code do what you need?

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

I meant "you assigned x to number", if y<number

OpenStudy (anonymous):

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

OpenStudy (anonymous):

Ok, now it makes more sense. Let me check it please.

OpenStudy (anonymous):

Sure, sorry about that, glad you caught it.

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

Exactly, you've got it. :)

OpenStudy (anonymous):

Well done, man! Or girl, I don't know. Anyway thanks again :D

OpenStudy (anonymous):

Your welcome

OpenStudy (anonymous):

:)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Twaylor: I wrote a song a while back (No AI used), and I just found it:
19 hours ago 17 Replies 3 Medals
luvnickk: how do we delete accounts on here?
1 day ago 2 Replies 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!