Python Forum
John Guttag Book Finger Exercise 2.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
John Guttag Book Finger Exercise 2.
#1
Hi,
I am learning Python using John Guttag's book - Introduction to Computation and Programming Using Python with Application to Understanding Data.

Finger Exercise 2 of the book states 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.”

Here's what I have come up with. Please note that the book expects the reader to write the program with the concepts taught till this exercise, which excludes using a list, sorting etc. I am to only use if/elif , input, modulo operator etc.

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

x = int( input ( "Enter x: " ) )
y = int( input ( "Enter y: " ) )
z = int( input ( "Enter z: " ) )

if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:
    print ("No odd numbers")
elif x % 2 == 0 and y % 2 == 0 and z % 2 != 0:
    print ("z: ", z , " is the greatest odd number amongst", x, y, z)
elif x % 2 == 0 and y % 2 != 0 and z % 2 == 0:
    print ("y: ", y , " is the greatest odd number amongst", x, y, z)
elif x % 2 != 0 and y % 2 == 0 and z % 2 == 0:
    print ("x: ", x, " is the greatest odd number amongst", x, y, z)


elif x % 2 == 0 and y % 2 != 0 and z % 2 !=0:
    if y > z:
        print ("y:", y , "is the greatest odd number amongst", x, y, z)
    else:
        print ("z:", z , "is the greatest odd number amongst", x, y, z)
        

elif x % 2 != 0 and y % 2 != 0 and z % 2 == 0:
    if x > y:
        print ("x:", x , "is the greatest odd number amongst", x, y, z)
    else:
        print ("y:", y , "is the greatest odd number amongst", x, y, z)

elif x % 2 != 0 and y % 2 == 0 and z % 2 != 0:
    if x > z:
        print ("x:", x , "is the greatest odd number amongst", x, y, z)
    else:
        print ("z:", z , "is the greatest odd number amongst", x, y, z)

elif x % 2 != 0 and y % 2 != 0 and z %2 != 0:
    if x > y and x > z:
        print ("x:", x , "is the greatest odd number amongst", x, y, z)
    elif y > z:
        print ("y:", y , "is the greatest odd number amongst", x, y, z)
    else:
        print ("z:", z , "is the greatest odd number amongst", x, y, z)
At first glance, the problem seemed easier. I had written a different script, but later as I found out, if I gave two even numbers, the script would show wrong output. Therefore I added so many checks.

I will be thankful if someone could let me know if there's a better way to write this.
Reply
#2
Hello and welcome to Python and the forums! Thanks for posting your code (and using code tags) Thumbs Up I moved the thread to Completed Scripts/Snippets subforum, as this is the place for posting working code and code reviews. Wink
Reply
#3
Hi J.crater,
Thank you.

For the record, here is the buggy script I had written the first time around. I tried to edit the original post, but it would not allow me to do so.

As can be seen below, for the input:
Enter x: -7
Enter y: -5
Enter z: 2

It prints:
No odd numbers

Which is wrong. That's the reason why I had to explicitly write the script with so many conditions.
me@ubuntu-machine:~/jguttag$ more buggy_finger_ex2_largest_odd.py 
#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.

x = int( input ( "Enter x: " ) )
y = int( input ( "Enter y: " ) )
z = int( input ( "Enter z: " ) )

if x > y and x > z and x % 2 != 0:
    print ("x:", x , "is the greatest odd number amongst", x, y, z)
elif y > z and y % 2 != 0:
    print ("y:", y , "is the greatest odd number amongst", x, y, z)
elif z % 2 != 0:
    print ("z:", z , "is the greatest odd number amongst", x, y, z)
else:
    print ("No odd numbers")


me@ubuntu-machine:~/jguttag$ python3 buggy_finger_ex2_largest_odd.py 
Enter x: -9
Enter y: -7
Enter z: 7
z: 7 is the greatest odd number amongst -9 -7 7
me@ubuntu-machine:~/jguttag$ python3 buggy_finger_ex2_largest_odd.py 
Enter x: -7
Enter y: -5
Enter z: 2
No odd numbers
me@ubuntu-machine:~/jguttag$
Reply
#4
I also got a long ass code because I wasn't sure how to shorten my code!
But I think you also have to consider the case where x, y, z are all the same odd numbers.

This is the code I wrote.

x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
z = int(input("Enter a value for z: "))

if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:    # Check if all numbers are even
    print('None of the x, y, z are ood!')
    
elif y < x and z < x: 
    if x % 2 != 0:    # Check if x is the biggest and odd
        print('x is the largest odd number!')
    else:     
        if y < z and z % 2 != 0:    # Compare y and z and see which one's odd
            print('z is the largest odd number!')
        elif z < y and y % 2 != 0:
            print('y is the largest odd number!')
        else:    # Used else because if both y and z were even, if statement above would've got it
            print('y and z, both are the largest odd numbers!')


elif x < y and z < y:
    if y % 2 != 0:
        print('y is the largest odd number!')
    else: 
        if x < z and z % 2 != 0:
            print('z is the largest odd number!')
        elif z < x and x % 2 != 0:
            print('x is the largest odd number!')
        else:
            print('x and z, both are the largest odd numbers!')
        
elif x < z and y < z:
    if z % 2 != 0:
        print('z is the largest odd number!')
    else: 
        if y < x and x % 2 != 0:
            print('x is the largest odd number!')
        elif x < y and x % 2 != 0:
            print('y is the largest odd number!')
        else:
            print('y and z, both are the largest odd numbers!')
        
else:    # If none of the conditionals are true, they're all the same and odd
    print('all numbers are the same odd numbers!')
Reply
#5
I am doing the same thing. Considering this book is my first and only exposure to Python, I don't really know what I'm doing.

Anyways, this is what I came up with. I tried to keep it as simple as possible.




x = int(input("Pick a number: "))
y = int(input("Pick another number: "))
z = int(input("Last one: "))

if x%2 == True: x = x
else: x = 0
if y%2 == True: y = y
else: y = 0
if z%2 == True: z = z
else: z = 0

n = x+y+z
if n == 0: 
    print ("You did not give me an odd number!")
else: print("The largest odd number you picked is ", max(x,y,z))
Reply
#6
Hello @Basalty, I did this exercise and I got it to work. However, my code is too long, could you try to tell me where I can find more information about the formulas that you used or try to explain the way you did your code. It worked and it is pretty short. Thanks.

Here you can see my loong code:

x = int (input("Put the X number: "))
y = int (input("Put the Y number: "))
z = int (input("Put the Z number: ")) 
  
if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:
    print ()
    print ("None of the entered number is a Odd number.")
    
else:
    
    if x % 2 == 1 and y % 2 == 1 and z % 2 == 1:
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
            
    elif x % 2 == 0 and y % 2 == 1 and z % 2 == 1:
        x = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
            
    elif x % 2 == 1 and y % 2 == 0 and z % 2 == 1:
        y = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
            
    elif x % 2 == 1 and y % 2 == 1 and z % 2 == 0:
        z = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
            
    elif x % 2 == 0 and y % 2 == 1 and z % 2 == 0:
        z = 0
        x = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
        
    elif x % 2 == 0 and y % 2 == 0 and z % 2 == 1:
        y = 0
        x = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
            
    elif x % 2 == 1 and y % 2 == 0 and z % 2 == 0:
        y = 0
        z = 0
        if x > y and x > z:
            print ()
            print('x is the largest Odd number.')
        elif y > z :
            print ()
            print('y is the largest odd number.')
        else:
            print ()
            print ('z is the largest odd number.')
Reply
#7
Just an FYI.
John Guttag's book is a classic, well written, etc. (I took his MIT class a few years back) This is just a heads up, that the book was published in 2016, which is a long time ago when it comes to computer science.
The concepts introduced are still sound, and I do not want to take away from the quality of this book in any way.
I am solely trying to inform you, so that you will look for more modern solutions to the exercises.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  John Guttag Book - Finger Exercise 3 pritesh 3 7,953 Aug-15-2018, 03:40 AM
Last Post: collectorKim
  John Guttag Python Book Finger Exercise 5 pritesh 0 8,209 Mar-04-2018, 09:08 PM
Last Post: pritesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020