2

(I added the function in question @ Bottom)

Working on my first python app.

Im making a simple rock, paper, scissors game.

I have a function that takes in two parameters:

  1. The users input
  2. The computers input

The function looks like this:

seeIfUserWon(userInput, selectRandomOption()) where selectRandomOption() just chooses a random Enum (ROCK, PAPER, SCISSORS)

The code works if I replace selectRandomOption with hardcoded input.


Currently that code gives me the error:

ValueError: 'Paper' is not a valid RPS where RPS is an Enum class with 3 cases: ROCK, PAPER, SCISSORS

But like I said if I just put in RPS.ROCK for the second parameter then it works...

What am I not understanding about python.. maybe I need to use a lambda here but I'm not sure? Any references would be appreciated as well!


Functions I used:

def selectRandomOption() -> RPS:
 pick = options[random.randint(0,2)]
 return RPS(pick)

Options:

options = ["Rock", "Paper", "Scissors"]

Which I then validate with:

def validateUserInput(input):
 if "rock" in input.lower():
 return RPS.ROCK
 elif "paper" in input.lower():
 return RPS.PAPER
 elif "scissors" in input.lower():
 return RPS.SCISSORS
 else:
 raise Exception

Enum :

class RPS(Enum):
 ROCK = auto()
 PAPER = auto()
 SCISSORS = auto()
asked Aug 15, 2021 at 0:54
4
  • 1
    What do you think RPS(pick) is supposed to work? Also, why not just put the enums in the list in the first place? Commented Aug 15, 2021 at 1:07
  • @juanpa.arrivillaga Thats why I'm here asking the question.. and I wanted to learn about enum conversion Commented Aug 15, 2021 at 1:10
  • 2
    You're calling RPS with a string argument, but it only takes integer arguments (that's what auto() does, it's assigning the integers 1-3 to your Enums. Either 1) give your Enums string values (i.e.: ROCK = "rock") so that you can simply do RPS("rock") or put the Enums themselves into the list from which you're randomly choosing: choice((RPS.ROCK, RPS.PAPER, RPS.SCISSORS)). Commented Aug 15, 2021 at 1:11
  • 1
    btw, I'd suggest using a different variable name than input as that is the name of a built-in function and you're overwriting it Commented Aug 15, 2021 at 1:14

3 Answers 3

4

Rather than randomly picking an integer, then looking up a string, then getting an enum value, consider just choosing the enum value directly. For example:

import random
class RPS(Enum):
 ROCK = auto()
 PAPER = auto()
 SCISSORS = auto()
random.choice(list(RPS))
# <RPS.SCISSORS: 3>
answered Aug 15, 2021 at 1:11
Sign up to request clarification or add additional context in comments.

3 Comments

There are no currently downvoted answers @madbird i.imgur.com/FgiPbNQ.png
Thanks that worked.. was my function selectRandom... not returning the right case.. ?
@SergioBost it wasn't really an issue of case. To get an enum by calling it (i.e. with (...), you need to pass the value not the name. These are integers the way you are using it. You could do something like RPS['ROCK'], but as others showed RPS(1) is probably better, or changing the values to strings. Still, I think it's nicer just to use choice().
2

It's tricky to say for sure without seeing the seeifuserwon function but I think this is probably what's going on.

If you rewrite this function as below:

def selectRandomOption() -> RPS:
 pick = random.randint(1, 3)
 return RPS(pick)

I suspect that will fix your problem... You've defined RPS as an Enum class. To get the corresponding class member for a value you need to pass the number to the Enum class - details here.

I also changed it to (1, 3) in the above since the count starts from 1 not 0 in the Enum class.

Dharman
34k27 gold badges106 silver badges158 bronze badges
answered Aug 15, 2021 at 1:07

1 Comment

You also could use random.choice as well
0

Try this:

class RPS(Enum):
 ROCK = "Rock"
 PAPER = "Paper"
 SCISSORS = "Scissors"
answered Aug 15, 2021 at 1:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.