I can input 2 strings, a or b they could be either rock,scissors, lizard,paper or spock.
their relationship is:
JavaScript
x
17
17
1
rock> scissors or rock> lizards
2
paper> rock or paper > spock
3
scissors>paper or scssors>lizard
4
lizard>spock or lizard> paper
5
spock>scissors or spock>rock
6
7
a=input("input move")
8
b=input("input another move")
9
10
if a==b:
11
print(0)
12
elif a>b:
13
print(1)
14
else:
15
print(2)
16
17
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:
JavaScript
1
23
23
1
series = ['rock','scissors','paper','lizard','spock']
2
3
winner = [
4
# rock scissors paper lizard spock
5
[ 1, 1, 0, 1, 0 ], # rock
6
[ 0, 1, 1, 1, 0 ], # scissors
7
[ 1, 0, 1, 0, 1 ], # paper
8
[ 0, 0, 1, 1, 1 ], # lizard
9
[ 1, 1, 0, 0, 1 ] # spock
10
]
11
12
while True:
13
op1 = input("Enter first type: ")
14
if op1 == 'quit':
15
break
16
op2 = input("Enter second type: ")
17
i1 = series.index(op1)
18
i2 = series.index(op2)
19
if winner[i1][i2]:
20
print( op1, "beats", op2 )
21
else:
22
print( op2, "beats", op1 )
23