首先要找到两个数字的"lcm"我做了一个函数lcm(a, b).然后我想找到"hcf",所以我做了一个装饰器decor并hcf(a, b)在其中定义了一个函数.然后我通过输入函数的名称返回此函数,我没有使用括号,但它仍然有效.我不明白为什么这个功能工作,即使我没有使用括号.
def decor(lcm_arg): # just to practice decorators
def hcf(a, b):
if a > b:
a, b = b, a
while True:
if b % a == 0:
print("hcf is", a)
break
else:
a, b = b % a, a
return lcm_arg(a, b)
return hcf # how hcf function is working without using brackets
@decor
def lcm(a, b):
if a > b:
a, b = b, a
for x in range(b, a*b+1, …Run Code Online (Sandbox Code Playgroud) 在学习 udemy 课程后,我陷入了“'str object is not callable”错误。我基本上已经复制并粘贴了给我带来问题的代码片段,但它仍然产生相同的错误。还没有发现任何与我遇到的确切问题相关的内容,但如果这是一个简单得可笑的修复,我不会感到惊讶!
问题在于“ops_function”位抛出“str”对象不可调用错误。
如果您需要更多信息,请告诉我,感谢您的见解!
from art import logo
print(logo)
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
operations = {
"+": "add",
"-": "subtract",
"*": "multiply",
"/": "divide"
}
first = int(input("Enter a number: "))
second = int(input("Enter another number: "))
for op in operations:
print(op)
op_choice = input("Select an operator from the list above: …Run Code Online (Sandbox Code Playgroud)