Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,463 PointsRock Paper Scissors in Python
How can I facilitate the actual logical comparison of the computer's random choice and the user's selection in a Python version of Rock Paper Scissors? I understand that string and integers are not directly comparable, I understand how to generate random integers with the computer. I've tried using a dictionary and translating the computer's random integer into a string and the user's selection into an integer. Nothing works, and I am starting to feel that I am just not smart or logical enough to program well. My instructor has warned us off internet solutions and I am trying so hard not to steal code or otherwise cheat. If one of you much more skilled people could describe the translation I think I can handle the control structures involved.
Thank you.
5 Answers
Patrick Cooney
12,216 PointsI don't want to give you the code answer but here is the data structure you will need. It's called an enum. It allows you to assign number values to words. See if you can use this to achieve your goal. If not, come back here, explain the problem a little deeper and we'll see if we can't get you a solution.
Daniel Schirmer
4,364 PointsI would just use a dictionary. You would have string keys, and number values. You can look up integer values which are related to the keys, and since the keys are strings, you can test user input against them.
Patrick Cooney
12,216 PointsIn this particular case a dictionary would add unnecessary complexity to the solution. That is if I'm correctly understanding the problem Nancy is attempting to solve.
Nancy Melucci
Courses Plus Student 36,463 PointsI tried that relatively early in the process and could not make it work. That might be my lack of skill though. I am taking a break now from it and will go back an take another whack using an enum and re-trying the dictionary if that doesn't work. Thanks for taking the time, I appreciate it.
Daniel Schirmer
4,364 PointsTaking a break is a great idea.
Also, Enum does literally what you asked, but it's probably not the best solution to your overall problem. Enums are a very specific tool, and your overall problem is fairly generic. You might want to re-consider other ways to get at what you need to do.
Patrick Cooney
12,216 PointsDaniel, I'm curious why you think an enum is too specific to solve this problem considering you stated it "does literally what you asked"?
Daniel Schirmer
4,364 PointsEnums are a very specific tool, and her overall problem is fairly generic. Dictionaries are pretty basic, and have broad usefulness. Given that she is making a basic rock paper scissors game, it seems more likely to me that her solution will lie in using dictionaries and/or approaching the problem differently, than in using enums.
Patrick Cooney
12,216 PointsThat doesn't make sense. Let's say you have a screw that needs to go into a wall. You have a screw driver which fits the purpose perfectly and you also have a more generic hammer. It's a hammer so it can do the same job but it's going to be far less efficient and make a mess of your wall. Are you going to use a hammer because the goal to "get it into the wall" is generic vs. "twist it into the wall" which is more specific? Sounds kinda silly when it's reframed that way doesn't it? That's essentially the point you're making. Enums are the most efficient tool for this job. You can do it with a dictionary, sure. But you'd have to write more code and be very verbose. Right tool for the job.
Nancy Melucci
Courses Plus Student 36,463 PointsThanks. I actually finished it using random choice and comparing strings. I would still like to see if it's possible to do this with a dictionary, and I'd also like to expand it into the rock paper scissors lizard spock version....later (after the term is over.) I appreciate your help and am learning a lot by following this question thread.
Nancy Melucci
Courses Plus Student 36,463 PointsNancy Melucci
Courses Plus Student 36,463 PointsThank you so much. I'll give it a try.