Python 我哪里做错了?取2个整数,传递函数,计算立方体之和
代码是:从用户处获取2个数字。将这两个数字传递给函数Python 我哪里做错了?取2个整数,传递函数,计算立方体之和,python,Python,代码是:从用户处获取2个数字。将这两个数字传递给函数mathExp()。此函数将计算两个数的立方体之和。 返回并打印此值。 功能结构: int mathExp(int,int) 我的代码是: num1 = int(input("write a number: ")) num2 = int(input("write a number: ")) def mathExp(num1,num2): print(num**3 + num**2, mathExp(num1,num2)) 你的ma
mathExp()
。此函数将计算两个数的立方体之和。
返回并打印此值。
功能结构:
int mathExp(int,int)
我的代码是:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(num1,num2):
print(num**3 + num**2, mathExp(num1,num2))
你的mathExp函数是错误的。请尝试以下方法:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(num1,num2):
ans = num1**3 + num2**3
return ans
cubes = mathExp(num1, num2)
print(cubes)
请尝试以下代码:
# take two number as input from the user
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
# define the function which calculate the sum of cubes of the input numbers
def mathExp(num1,num2):
result = num1**3 + num2**3
return result
# call the function with user input and print the result
print(mathExp(num1, num2))
例如:
write a number: 4
write a number: 7
407
此代码有很多错误: 首先,您正在调用mathExp函数本身。这是递归,我不认为你想这样做。 其次,mathExp函数的参数被称为
num1
和num2
。但是在函数中,您只使用了不存在的num
所以你可能想做的是:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(n1,n2):
return n1**3 + n2**3
print(mathExp(num1, num2))