0

so my code has a lot of if statements:

            rnrp += 1
        if a100 == b100:
            rnrp += 1
        if a10 == b10:
            rnrp += 1
        if a1 == b1:
            rnrp += 1

and:

            if b1000 == a100:
                rnwp += 1
                break
            if b1000 == a10:
                rnwp += 1
                break
            if b1000 == a1:
                rnwp += 1
            break
        while true:
            if b100 == a1000:
                rnwp += 1
                break
            if b100 == a10:
                rnwp += 1
                break
            if b100 == a1:
                rnwp += 1
            break
        while true:
            if b10 == a1000:
                rnwp += 1
                break
            if b10 == a100:
                rnwp += 1
                break
            if b10 == a1:
                rnwp += 1
            break
        while true:
            if b1 == a1000:
                rnwp += 1
                break
            if b1 == a100:
                rnwp += 1
                break
            if b1 == a10:
                rnwp += 1
            break 

as you can see this is a lot of if statements, the first is fine, but the second needs improvements. Also what the second code is trying to accomplish is checking each place of b (100's place, 100's place, 10's place etc..) matches any of the places of a. How can I shrink the amount of if statement's in the second part of the code? this post has been answered by jasonharper (I cant find the button as of right now so I'm just putting it in the question) thanks!!

6
  • "each place of b (100's place, 100's place, 10's place etc..) matches any of the places of a" This is not really enough context to understand the problem properly. What overall problem are you trying to solve with this code? Commented Oct 26, 2020 at 0:04
  • 2
    When performing similar operations on many variables, it may be better to put those variables in a collection (list, dict, numpy, pandas, etc...) than to use them individually. As an aside, those while's with unilateral breaks in them are unneeded. You could just do if, elif ... instead. Commented Oct 26, 2020 at 0:07
  • It's totally dependent on the program's logic. As a first workaround, you can encapsulate each set of the related conditions in function, so instead of multiple if statements you'll only call the corresponding function. Commented Oct 26, 2020 at 0:08
  • Each while True: block, with three ifs, could be replaced by if/elif/elif, getting rid of all the breaks. They could be further shortened by writing if b1000 in (a100, a10, a1): rnwp += 1. Commented Oct 26, 2020 at 0:10
  • What's the point of a loop if you always break out of it immediately? Commented Oct 26, 2020 at 0:13

4 Answers 4

1

You can use collections like list or dict to reduce the number of operations. For instance,

    while true:
        if b100 == a1000:
            rnwp += 1
            break
        if b100 == a10:
            rnwp += 1
            break
        if b100 == a1:
            rnwp += 1
        break

Could be

        if b100 in [a1000, a10, a1]:
            rnwp += 1

Since you are repeating some of these patterns often, you could make some of these lists before hand and repeat their usage. It may even be useful to use dictionaries instead so that you don't have to remember the variables separately.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the elif statements. Other than that there's not too much more you can do there. Using many if statements can be confusing but as long as your code is clean then you can pull it off.

1 Comment

It won't help make it shorter
0

The way I would shortened it:

Boolean_value = True
while Boolean_value:
    if b100 == a1000: rnwp, Boolean_value = rnwp + 1, False 
    if b100 == a10: rnwp, Boolean_value = rnwp + 1, False 
    if b100 == a1: rnwp, Boolean_value = rnwp + 1, False 

It's pretty useful, if you want to an if statement and that if statement only have one expression. You can change multiple values in one line as well.

Comments

0

You could use product from itertools and do it all on a single line:

from itertools import product 

rnwp += sum(b==a for b,a in product([b1000,b100,b10,b1],[a1000,a100,a10,a1]))

Comments

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.