문제

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."
도움이 되었습니까?

해결책

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.

다른 팁

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")

This is inspired by Eric's answer but meets your need to print x, y and z instead of values.

>>>val={'x':x,'y':y,'z':z}
>>>newval=dict(k,d for k,d in val.items() if d%2==1) 

Personally I would prefer to write if d%2 but that's just me, it's up to you.

>>>if newval:
          print(max(newval.items(),key=lambda x:x[1]),'is maximum')
   else:
          print('No odd numbers')

It uses a dictinoary with Eric's list comprehension idea.

EDIT: I just figured that this would be cooler using a function.

>>>def findmax(**x):
   new=dict((k,d) for k,d in x.items() if d%2)
   if new:
       print(max(new.items(),key=lambda x:x[1]),'is maximum')
   else:
       print('No odd numbers')

Test run like so,

>>>findmax(x=1,y=37,z=208,a=193)
(a,193) is maximum
nums = x, y, z

try:
    print max([n for n in nums if n%2 == 1])
except ValueError:
    print 'No odd numbers'

This prints 'No odd numbers' if there are none in nums

Doing print(max(some_list)) where some_list is empty returns an error and so is not dealing with situations where there are no odd numbers present

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top