anonymous
  • anonymous
I'm struggling with the finger test on page 16 of the book, 'Write a program that examines three variables-x, y and z and prints out the largest odd number amongst them. If none are odd, it should print a message to that effect.' My problem is getting the program to discard even numbers and continue to evaluate the odd ones. Also not sure how I post the code I've already written..
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
##first assign values to X,Y and Z x = int(raw_input('Enter an odd value for X: ')) y = int(raw_input('Enter an odd value for Y: ')) z = int(raw_input('Enter an odd value for Z: ')) ##evaluate if there is an even number amongst them if x%2 ==0: print 'You have an even number' if y%2 ==0: print 'You have an even number' if z%2 ==0: print 'You have an even number' ##if there is no even number, go ahead and calculate the largest else: if x > y and y > z: print 'X is the largest' elif y > z: print 'Y is the greatest' else: print ' Z is the greatest'
e.mccormick
  • e.mccormick
I am not sure which book you have or how much coding you know, but it seems like this one is supposed to be a very basic problem. If you have done arrays, comprehensions, or loops, this can be done exceptionally easy. If you only know how to set a variable and use if/elif/else, well, it is a bit harder. Right now you are testing to find the largest no matter if they are all even or not. Your outer set of if/elif/else needs to weed down which ones are tested. If you know the other things, you could put your variables into an array based on if they pass the even test. If there is nothing in the array, print the none odd message. Otherwise, sort the array and print the largest, which will be the first or last depending on how you sort.
anonymous
  • anonymous
Thanks for the advice, I'm using 'Introduction to Computation and Programming Using Python' by John V. Guttag, the book recommended for the 6.00 course. I am completely new to coding, so yes, wrapping my head around if/elif/else is about as far as I have got.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • e.mccormick
OK. I never saw the inside of the Guttag book. With just if/elif/else: first filter out if you have evens or not. This will be a large if/elif structure for each combination of odd, odd, odd; odd, odd, even; odd, even, even; and so on. ``` if x%2==0 and y%2==0 and z%2==0: print "All are even!" ## Note: Now that we know that not all three are even, we can test for just two! elif x%2==0 and y%2==0: print z, "is the highest odd number." ## Note: Because I knew 2 were even, I just printed the third. ``` You keep going like that, looking at only one odd for the other two, then two odds and one has to be larger, so nested if/else for that, and finally the all three are odd case.
e.mccormick
  • e.mccormick
After you have solved this with what you already know, take a look at some more advanced solutions. The same question was asked, but the person did not say they only knew if/else type things, so we gave a range of answers! http://openstudy.com/study#/updates/521af4f4e4b06211a67d1325 As you learn more, you will find new ways to do things that make it simpler and faster!
anonymous
  • anonymous
Thanks for taking the time to explain that, and the link showing different methods to approach the problem is totally cool. Makes so much more sense now, much appreciated.
e.mccormick
  • e.mccormick
SPOLER ALERT! The below link is an answer to this! http://dpaste.com/hold/1362086/ If you can't work it out or have some trouble with all the if/elif/else chains, that is a solution using JUST that basic bit of code. In fact, the only things used are %, =, ==, raw_input(), int(), if, elif, else, and print.

Looking for something else?

Not the answer you are looking for? Search for more explanations.