0
Follow
2
View

Multiplication or sum of two numbers [closed]

easy200707 注册会员
2023-01-26 09:36

For python3.8+ code can be:

def sum(num1: int, num2: int) -> int:
    if (product := num1 * num2) <= 1000:
        return product
    return num1 + num2
dzkhswz 注册会员
2023-01-26 09:36

The expected output looks good to me.

But, in case you were wondering: can I do this in one line with python? the answer is yes:

def sum(num1, num2):
    return num1*num2 if num1*num2 <= 1000 else num1+num2
ycwc880924wc 注册会员
2023-01-26 09:36
if x*y <= 1000:
   return x*y
else:
   return x+y 

20 * 30 = 600
600 < 1000
return 600

50 * 10 = 500
500 < 1000
return 500