Set common conditions for if statements

Asked 2 years ago, Updated 2 years ago, 54 views

When setting the if statement condition, I would like to know if there is a way to set the common condition only once with one variable or another.

If you set it as a variable, true false is set as the condition when setting the variable for the first time, so I don't think you can use it as a variable.

# Example of a common variable
if1 = a * b > 0

# Conditional statement 1
a = -1
b = 1
if if1:
    # true operation
else:
    # false operation

a = 1
b = 1
if if1:
    # true operation
else:
    # false operation

if문 python

2022-09-20 10:48

1 Answers

That's why it's a function.

def both_positive_or_both_negative(a, b):
    return a * b > 0

if both_positive_or_both_negative(1, -1):
    print("test failed!")
else :
    print("test succeeded!")


2022-09-20 10:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.