简体   繁体   中英

Write a program that compares the three numbers and prints the largest odd number

I am trying to write a program that compares the three numbers and prints the largest odd number. Does this program below what you would write?

def largest_odd_number(x, y, z):
    """Write a program that compares the three numbers and prints the largest odd number"""
    odd_number_list = []
    try:
        if int(x) == x and x % 2 != 0:
            odd_number_list.append(x)
        else:
            if int(y) == y and y % 2 != 0:
                odd_number_list.append(y)
            else:
                if int(z) == z and z % 2 != 0:
                    odd_number_list.append(z)
        print(max(odd_number_list))
    except ValueError:
        print("No odd integers are found!")

To find the largest odd number among 3 arguments that pass to a function would look like

def largest_odd_number(x, y, z):
    """Write a program that compares the three numbers and prints the largest odd number"""
    odd_number_list = []
    try:
        for i in (x, y, z):
            if int(i) % 2 != 0:
                odd_number_list.append(i)
        print(max(odd_number_list)) if odd_number_list else print("No odd integers are found!")
    except ValueError:
        print("One of the arguments can't be cast to Int")

This function will iterate over the 3 args and will check if each one of them is an odd number and if so it's appended to the odd_number_list and in the end, it will print the largest number of the odd_number_list variables.
In addition, this function will check if the odd_number_list is empty after iterating over all args and if so it will print "No odd integers are found!" and in the except ValueError part, the print will be indicate that one of the args can't be cast to int as "One of the arguments can't be cast to Int" .

As mentioned in comments, you should remove the else parts. Other than that, here's another way to write the same function in a simpler way and to avoid iterating over the variables 2 times:

def largest_odd_number(x, y, z):
    """Write a program that compares the three numbers and prints the largest odd number"""
    max_odd = -1
    for n in [x, y, z]:
        if isinstance(n, int) and (n & 1) == 1 and n > max_odd:
            max_odd = n

    if max_odd == -1:
        print("No odd integers are found!")
    else:
        print(max_odd)

could you not just simplify this into a simple try/except with a list comprehension?

try:
    max_odd = max([num for num in [x,y,z] if int(num) % 2 != 0])
except:
    print('No odd numbers')

It's unnecessary to allocate extra space. Since you are receiving just 3 numbers, you can proceed to the comparison without the extra list. Also, I would like to know if your numbers have at least one that is odd, and if not, what should you return? Assuming your numbers are always positive, this code works:

def largest_odd_number(x, y, z):
    ans = 0
    if(x%2!=0):
        ans=x
    if(y%2!=0 and y>ans):
        ans=y
    if(z%2!=0 and z>ans):
        ans=z
    return ans

If you like lists and exceptions, you can use them:

def largest_odd_number(x, y, z):
  odd_number_list = [x for x in [ x, y, z ] if x%2 != 0 and isinstance(x, int)]
 try:
   print(max(odd_number_list))
 except ValueError:
   # if odd_number_list is empty, max() will raise ValueError
   print("No odd integers are found!")

>>> largest_odd_number(5,63.3,4)
5
>>> largest_odd_number(6,63.3,4)
No odd integers are found!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM