Reputation: 4200
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
Upvotes: 0
Views: 38
Reputation: 54649
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 )
Upvotes: 1