Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logical test of odd numbers

I wonder if my answer below is correct and feasible or are there any improvements Thank you. question: a program that checks x y and z if they are odd or even if they are odd checks if the number is the biggest of all three. Below my answer:

    x = 4
    y = 7
    z = 9

  #first condition where 3 numbers are all odd.

    if x%2 == 1 and y%2 == 1 and z%2 == 1:
        if x > y and x > z:
            print "x is the biggest odd number."
        elif x > y and z > x:
            print "z is the biggest odd number."
        else:
            print "y is the biggest odd number."

#second condition where 2 of the numbers are odd
elif x%2 == 0 and y%2 == 1 and z%2 == 1:
    if y > z:
        print "y is the biggest odd number."
    else:
        print "y is the biggest odd number."
elif x%2 == 1 and y%2 == 0 and z%2 == 1:
    if x > z:
        print "x is the biggest odd number."
    else:
        print "z is the biggest odd number."

elif x%2 == 1 and y%2 == 1 and z%2 == 0:
    if x > y:
        print "x is the biggest odd number."
    else:
        print "y is the biggest odd number."    

#third condition where only one of the numbers is odd.

elif x%2 == 0 and y%2 == 0 and z%2 == 1:
    print "z is the biggest odd number."
elif x%2 == 1 and y%2 == 0 and z%2 == 0:
    print "x is the biggest odd number."
elif x%2 == 0 and y%2 == 1 and z%2 == 0:
    print "y is the biggest odd number."

#last condition if none of the numer are odd or not numbers.

else:
    print " None of the numbers are odd or not a number."
like image 916
Snowman Avatar asked Mar 11 '26 05:03

Snowman


2 Answers

Wow! That is a ton of code. Why don't you just put x,y,z in a list. Use a list comprehension to remove even numbers. Then select the maximum value using the max function. Something like this

arr = [x,y,z]
arr = [i for i in arr if i%2!=0] #select only odd numbers
print(max(arr)) #display the maximum number

Then you can use max(arr) to figure out what variable contains the maximum odd number.

like image 69
Eric Urban Avatar answered Mar 13 '26 17:03

Eric Urban


Your solution is probably a bit too verbose and specific. What if you needed to find the largest odd number and there were four? Or a hundred?

A better idea would be to use Python's max function to examine a sequence that filters out even numbers.

You can collect the numbers to be examined in a sequence like a list:

A = [53, 31, 59, 75, 25, 32, 99, 15, 63, 35]

Then you can filter that sequence with a list comprehension:

A_odd = [n for n in A if n % 2 != 0]

Then you can find the max of that sequence:

max_odd = max(A_odd)

Suppose that there were no odd numbers in the sequence, though. The max function raises an exception if its argument is empty. So we can use a conditional expression to check to see if A_odd is empty:

if A_odd:
    max_odd = max(A_odd)
else:
    max_odd = "No odd numbers in sequence"

Then when we print the answer with print(A_odd) we get the result or the message.

Python 3.4 (not yet released) includes an addition to the max function that allows this to be even more compact - you can skip the conditional and just do max(A_odd, default="No odd numbers in sequence")

like image 44
bbayles Avatar answered Mar 13 '26 19:03

bbayles