Jump to content
  • Advertisement
Sign in to follow this  

How is this python statement suppose to be friendly? (3.4 ver)

This topic is 1206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I made this little fun code to review over what I've learned... Btw, this is only a snippet of the full code

player_class = input("Pick a class. Pick a choice between - Duelist,"
                     "Trap Master, and Gun Doctor")
#The code below is a copy and paste that I hacked to death to make i work with this...
if not re.match('^[1-3]*$', player_class):
    print("Error! Only numbers 1 to 3 are allowed!")

if player_class == 1:
    print("You picked the Gun Doctor")
elif player_class == 2:
    print("You picked the Trap Master")
    print("You picked the Assassin")

I was searching for a way to only allow the user to use specific characters, so I googled it, and I got this code...

if not re.match("^[a-z]*$", input_str):
    print "Error! Only letters a-z allowed!"

It probably looks similar to a statement that is in the previous code, and yes it is, because I hacked it off stack exchange and pasted it in my code, and then proceed to change the if statement. So instead of 

if not re.match("^[a-z]*$", input_str):

I changed the [a-z] to [1-3], and instead of input_str, I changed it into player_class.


Now my problem is, I don't understand it

if not re.match('^[1-3]*, player_class):

I understand that changing the a-z to 1-3 makes it so that I can only choose from 1 to 3, but the rest of the code is weird.. Like why is there a * and an ^ with an ' ... Like, what? Why do I need to put the player_class variable in there to? Can someone help me understand this rather weird piece of code?

Share this post

Link to post
Share on other sites

The '^' means "begins-with". The square brackets form a "closure" that means "any of". The '*' means zero or more occurrences of the previous pattern. So, all together, it means "match anything that begins with any character in the brackets followed by any number of occurrences of what's in the brackets".


It's called a "regular expression". Put that term in google and you will learn more than you ever want to know.

Share this post

Link to post
Share on other sites
It's a regular expression. Regular expressions are very powerful that can match and transform text. The pattern "^[1-3]*" is read as:

1. Beginning of line anchor, ^: Matches the start of the line.
2. Character set, [1-3]: Matches any character between 1 and 3.
3. Greedy match, *: Matches the previous expression at least 0 times, and matches as many as possible.

If you wanted to match just "1", "2", or "3", then you'd use "^[1-3]$".

You can learn more about regular expressions on this dedicated site: http://www.regular-expressions.info/ Edited by fastcall22

Share this post

Link to post
Share on other sites

As others have already explained, you stumbled upon regular expressions. Once you understand how to read them,they are not so complicated, but until that time... yeah.


Regular expressions are very powerful, way too powerful for the problem you are trying to solve here. There are much simpler solutions possible.

Let's try again:

player_class = input("Pick a class. Pick a choice between - Duelist, Trap Master, and Gun Doctor")

Fair enough, let's assume the player enters a letter ('d', 't', or 'g').


A straight-forward solution is to test for each value separately

if not (player_class == 'd' or player_class == 't' or player_class == 'g'):
    print("Error! Only 'd', 't', or 'g' are allowed!")

If it is not one of the good answers, it is wrong.

It works, but is a bit long-ish, testing the same variable against different values can be written in a more compact form:

if not (player_class in ('d', 't', 'g')):
    print("Error! Only 'd', 't', or 'g' are allowed!")

"player_class in ('d', 't', 'g')" takes the value in player_class, and tests it against the given three values.

The "not (...in ...)" reads very fuzzy, you can change it into

if player_class not in ('d', 't', 'g'):
    print("Error! Only 'd', 't', or 'g' are allowed!")

The 'in' function is reversed now, it tests whether player_class is not one of the given values. It reads much nicer imho smile.png


Selection is then something like

if player_class == 'g':
    print("You picked the Gun Doctor")

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!