两个数的乘法或求和

multiplication or sum of two numbers

给定两个整数,return只有他们的乘积 如果乘积等于或小于 1000,则 return 它们的总和。

Def multiplication_or_sum(num1, num2):
    product = num1 * num2
    if product < = 1000:
        return product
    else:
        return num1 + num2

result = multiplication_or_sum(20,30)
print("The result is = ", result)


result = multiplication_or_sum(50,10)
print("The result is = ", result)

我的输出

The result is =  600
The result is =  500

预期输出

The result is =  600
The result is =  60

但无法找出错误

if the product is equal to or lower than 1000, else return their sum.

if x*y <= 1000:
   return x*y
else:
   return x+y 

20 * 30 = 600
600 < 1000
return600

50 * 10 = 500
500 < 1000
return500

我认为预期的输出不错。

但是,如果您想知道:我可以用 python 一行完成吗?答案是肯定的:

def multiplication_or_sum(num1, num2):
    return num1*num2 if num1*num2 <= 1000 else num1+num2

对于python3.8+代码可以是:

def multiplication_or_sum(num1: int, num2: int) -> int:
    if (product := num1 * num2) <= 1000:
        return product
    return num1 + num2