Jan-28-2018, 08:28 PM
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.
I will be thankful if someone could let me know if there's a better way to write this.
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.