1

I have the following code;

if A:
    X = find()
else:
    X = find(all=True)

I need to add a new check - if C then find(amonut=C). In other words, the code would look like:

if A:
    if C:
         X = find(amount=C)
    else:
         X = find()
elif C:
    X = find(amount=C)
else:
    X = find(all=True)

It looks very ugly. Is there a better way to achieve the same behavior?

0

4 Answers 4

2

It looks to me that you find by amount "when C" irrespective of what A is, so make that the first thing to think about. Plus you can use elif to avoid deep nesting in this case:

if C:
     x = find(amount=C)
elif A:
     x = find()
else:
     x = find(all=True)
Sign up to request clarification or add additional context in comments.

Comments

1
if A and not C:
    X = find()
elif C:
    X = find(amount=C)
else:
    X = find(all=True)

Comments

1

Single line if/else

X = find(amount=C) if C else find() if A else find(all=True)

Comments

0

you can use Switcher instead of a bunch of if statements.

learn more about switcher

2 Comments

Providing the code demonstrating how it would be used for this case would improve this answer.
@Dave I agree with you. but in general, if statements could be converted to switcher only if they are trying to compare one argument with different values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.