+ 2

Easier way to do this? #SOLVED

CODE LINK: https://code.sololearn.com/cy8bO00Z8a64 I need to check all combinations of rock paper scissors game. I can do this with 9 if`s, but can i do it easier? if img == rock and img2 == rock: res.set("Tie!") if img == rock and img2 == paper: res.set('Computer won!') if img == rock and img2 == scissors: res.set('You won!')

28th Apr 2022, 1:08 PM
Artur
Artur - avatar
10 Réponses
+ 8
Hello Artur I would use a dictionary. Keys = Rock, Paper and Scissor. Values = looses against key. https://code.sololearn.com/c69g4Y8klwfZ/?ref=app
28th Apr 2022, 4:32 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
I'm sure this could be done with some type of lambda, map thingy where you have lists inside of lists where it returns false or true depending on the case.
28th Apr 2022, 3:50 PM
Wheres8
Wheres8 - avatar
+ 1
Thank you, its actually pretty smart and simple!
28th Apr 2022, 7:31 PM
Artur
Artur - avatar
+ 1
Artur Your welcome!
28th Apr 2022, 7:35 PM
Denise Roßberg
Denise Roßberg - avatar
0
Can I please see your entire code?
28th Apr 2022, 4:11 PM
Ava Alford
0
Denise, i have a question about line wins[you]==comp. Wins[you] is same as Wins["Rock"] so it returns scissors?
28th Apr 2022, 7:23 PM
Artur
Artur - avatar
0
Artur Yes. dictonary[key] = value So wins["Rock"] returns "Scissor", wins["Scissor"] returns "Paper" and wins["Paper"] returns "Rock"
28th Apr 2022, 7:27 PM
Denise Roßberg
Denise Roßberg - avatar
0
Whenever I run this code Artur it output is You Win!
29th Apr 2022, 5:42 AM
Ashwin Kumar
Ashwin Kumar - avatar
0
Ashwin Kumar gavet you changed the key? In this example 'you' is always rock, so output is same every time
29th Apr 2022, 7:52 AM
Artur
Artur - avatar
0
Ava Alford, you can see the whole code now, i included it to post. But you need to use tkinter and have 5 images in order to make it work
29th Apr 2022, 6:48 PM
Artur
Artur - avatar