Advertisements
Is there a way to store the output of the function but only if it isnt false, and not having to call the function 2 times as in the example?
def example(x):
if x>=5:
return "great"
else:
return False
def main():
x = 5
if example(x):
asnwer = example(x)
else:
print("x<5")
main()
>Solution :
def main():
x = 5
result = example(x)
asnwer = result if result else print("x<5")
You could also just modify the function:
def example(x):
return "Great" if x>=5 else print("x<5")
def main():
x = 5
asnwer = example(x)
main()
This would give asnwer == None
when x<5
and asnwer == Great
when x>=5
.