lundi 15 janvier 2018

break the for statement loop

Why the output for this code is:

DRAW
A WINS
A WINS
ERROR

and not

DRAW
A WINS
ERROR
ERROR

as it supposed to be?

Why break doesn't work and doesn't give a result = 'ERROR' for string with not allowed letter (strB = 'SRTR')

def RockPaperScissors(strA,strB):
A_win = 0
B_win = 0
allowed_letters = 'SRP'

if (len(strA) != len(strB)):
    result = 'ERROR'

elif (len(strA) == len(strB)):

    for char in strA:
        for c in strB:
            if char not in allowed_letters:
                result = 'ERROR'
                break
            elif c not in allowed_letters:
                result = 'ERROR'
                break

    for i in range(0,len(strA)):
        if (strA[i] == 'R' and strB[i] == 'S'):
            a_win = 1
            A_win += a_win
        if (strA[i] == 'S' and strB[i] == 'P'):
            a_win = 1
            A_win += a_win
        if (strA[i] == 'P' and strB[i] == 'R'):
            a_win = 1
            A_win += a_win
        if (strB[i] == 'R' and strA[i] == 'S'):
            b_win = 1
            B_win += b_win
        if (strB[i] == 'S' and strA[i] == 'P'):
            b_win = 1
            B_win += b_win
        if (strB[i] == 'P' and strA[i] == 'R'):
            b_win = 1
            B_win += b_win   

    if A_win > B_win:
        result = 'A WINS'
    if B_win > A_win:
        result = 'B WINS'
    if A_win == B_win:
        result = 'DRAW'

return result

print(RockPaperScissors('RP','PR'))
print(RockPaperScissors('PPP','RRR'))
print(RockPaperScissors('RPSR','SRTR'))
print(RockPaperScissors('RPS','RPSRP'))

Aucun commentaire:

Enregistrer un commentaire