Skip to content
Advertisement

how to compare two strings in multiple cases?

I can input 2 strings, a or b they could be either rock,scissors, lizard,paper or spock.

their relationship is:

rock> scissors or rock> lizards
paper> rock or paper > spock
scissors>paper or scssors>lizard
lizard>spock or lizard> paper
spock>scissors or spock>rock

a=input("input move")
b=input("input another move")

if a==b:
 print(0)
elif a>b:
 print(1)
else:
print(2)

how to write this program?

Thanks

Advertisement

Answer

I would do it by making a win/loss matrix. The matrix contains a 1 where the row beats the column:

series = ['rock','scissors','paper','lizard','spock']

winner = [
#     rock   scissors  paper  lizard  spock
    [  1,      1,       0,     1,      0  ],  # rock
    [  0,      1,       1,     1,      0  ],  # scissors
    [  1,      0,       1,     0,      1  ],  # paper
    [  0,      0,       1,     1,      1  ],  # lizard
    [  1,      1,       0,     0,      1  ]   # spock
]

while True:
    op1 = input("Enter first type: ")
    if op1 == 'quit':
        break
    op2 = input("Enter second type: ")
    i1 = series.index(op1)
    i2 = series.index(op2)
    if winner[i1][i2]:
        print( op1, "beats", op2 )
    else:
        print( op2, "beats", op1 )
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement