Multiplication or sum of two numbers
Question:
Given two integer numbers, return their product only
if the product is equal to or less than 1000, else return their sum.
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)
my output
The result is = 600
The result is = 500
expected output
The result is = 600
The result is = 60
but I can’t figure out the error.
Answers:
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
return 600
50 * 10 = 500
500 < 1000
return 500
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 multiplication_or_sum(num1, num2):
return num1*num2 if num1*num2 <= 1000 else num1+num2
For python3.8+ code can be:
def multiplication_or_sum(num1: int, num2: int) -> int:
if (product := num1 * num2) <= 1000:
return product
return num1 + num2
Given two integer numbers, return their product only
if the product is equal to or less than 1000, else return their sum.
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)
my output
The result is = 600
The result is = 500
expected output
The result is = 600
The result is = 60
but I can’t figure out the error.
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
return 600
50 * 10 = 500
500 < 1000
return 500
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 multiplication_or_sum(num1, num2):
return num1*num2 if num1*num2 <= 1000 else num1+num2
For python3.8+ code can be:
def multiplication_or_sum(num1: int, num2: int) -> int:
if (product := num1 * num2) <= 1000:
return product
return num1 + num2