dimanche 25 octobre 2020

how do i reduce the amount of if statements in my code?

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!!

Aucun commentaire:

Enregistrer un commentaire