此处的elif someother_function():检查它是否为真,并且还需要打印返回的值。有人能帮我吗?
def some_function():
if correct:
return True
else:
return False
def someother_function():
if correct:
return True,f
else:
return False,None
if some_function():
print("something")
elif someother_function():
print(f)
else:
print("nothing")在这里,如何将f从someother_function返回到它在elif调用的位置?
发布于 2021-01-08 06:49:48
你可以试试:
def some_function(correct):
if correct:
return False
else:
return False
def someother_function():
if correct:
return True, f
else:
return False, None
if some_function():
print("something")
elif [x := someother_function()] and x[0]:
print(x[1])
else:
print("nothing")发布于 2021-01-08 07:06:04
如果需要,可以用函数编写类,并用输出设置类变量:
import random
class foo:
def __init__(self):
self.f=None
def some_function(self):
correct= random.choice([True, False])
if correct:
return True
else:
return False
def someother_function(self):
self.f = "something 2"
correct= random.choice([True, False])
if correct:
return True,
else:
return False,None
if __name__=="__main__":
temp = foo()
if temp.some_function():
print("something")
elif temp.someother_function():
print(temp.f)
else:
print("nothing")发布于 2021-01-08 07:42:27
我认为你需要指定一种事件。如果需要,请检查这段代码并尝试它:
import random
event = random.choice([True, False])
def some_function(correct):
if correct:
return True
else:
return False
def someother_function(correct):
if correct:
return True, f
else:
return False, None
if event == True:
some_function()
print("something")
elif event == False:
someother_function():
print(f)
else:
print("nothing")https://stackoverflow.com/questions/65624470
复制相似问题