[Tutor] Better way to compare values?

memilanuk memilanuk at gmail.com
Mon Aug 29 03:19:05 CEST 2011


Hello,

Recently picked back up the Python bug, and have started chipping away 
at the warm-up exercises over @ codingbat.com.

I'm getting thru the basic stuff okay, but I'm getting a feeling that 
maybe I'm not doing things the 'right' way, even though the results are 
correct.

Specifically, checking if value 'a' OR value 'b' equal a certain number...

The example that I'm on at the moment (like I said, starting over at the 
basics)...

http://codingbat.com/prob/p124984

My solution:

'''
Given 2 ints, a and b, return True if one if them is 10 or if their sum 
is 10.

makes10(9, 10) = True
makes10(9, 9) = False
makes10(1, 9) = True
'''

def makes10(a, b):
     if (a == 10) or (b == 10):
         #print('Yep!')
         return(True)

     elif a + b == 10:
         #print('Si!')
         return(True)

     else:
         #print('Nein!')
         return(False)

makes10(10,9)
#makes10(9,9)
#makes10(1,9)



In particular, the 'if (a == 10) or (b == 10): line... is there a 
shorter/more compact/more correct (i.e. pythonic) way of testing to see 
if a OR b is equal to 10?

Thanks,

Monte



More information about the Tutor mailing list