I am trying to make a simple program in Python that calculates the largest odd number out of the values x, y, z. How do I give the user an option to pick the values for x, y, and z?
So the program will ask what x, y, and z is and then say "x,y,z is the largest odd" or the numbers are all even.
What I have so far is below. Is this at least a decent start?
# This program exmamines variables x, y, and z
# and prints the largest odd number among them
if x%2 !== 0 and x > y and y > z:
print 'x is the largest odd among x, y, and z'
elif y%2 !== 0 and y > z and z > x:
print 'y is the largest odd among x, y, and z'
elif z%2 !== 0 and z > y and y > x:
print 'z is the largest odd among x, y, and z'
elif x%2 == 0 or y%2 == 0 or z%2 == 0:
print 'even'
With thkang post, I now have:
# This program exmamines variables x, y, and z
# and prints the largest odd number among them
if x%2 !== 0:
if y%2 !== 0:
if z%2 !== 0:
if x > y and x > z: #x is the biggest odd
elif y > z and y > x: #y is the biggest odd
elif z > x and z > y: #z is the biggest odd
else: #z is even
if x > y: #x is the biggest odd
else: #y is the biggest odd
else: #y is even
if z%2 != 0: #z is odd
if x > z: #x is the biggest odd
else: #z is the biggest odd
else: #y,z are even and x is the biggest odd
else: #x is even
if y%2 != 0 and z%2 != 0; #y,z is odd
if y > z: #y is the biggest odd
else: #z is the biggest odd
else: #x and y is even
if z%2 != 0: #z is the biggest odd
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: Largest N-digit even number will be (10n) – 2 because the series for different values of N will be 8, 98, 998, 9998, ….. Similarly, largest N-digit odd number will be (10n) – 1 for the series 9, 99, 999, 9999, …..
num = int (input (“Enter any number to test whether it is odd or even: “) if (num % 2) == 0: print (“The number is even”) else: print (“The provided number is odd”) Output: Enter any number to test whether it is odd or even: 887 887 is odd. The program above only accepts integers as input.
Avoid using if-stmts
to find maximum. Use python builtin max
. Use either generator or filter
to find only the odd numbers.
Using builtins like this is safer/more reliable because it is simpler to compose them, the code is well-tested, and the code executes mostly in C (rather than multiple byte code instructions).
def find_largest_odd(*args):
return max(arg for arg in args if arg & 1)
or:
def find_largest_odd(*args):
return max(filter(lambda x: x & 1, args))
>>> def find_largest_odd(*args):
... return max(arg for arg in args if arg & 1)
...
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1
and:
>>> def find_largest_odd(*args):
... return max(filter(lambda x: x & 1, args))
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1
If you pass an empty sequence or provide only even numbers, you will get a ValueError
:
>>> find_largest_odd(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in find_largest_odd
ValueError: max() arg is an empty sequence
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With