Advertisement

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

Started by August 28, 2015 02:12 AM
5 comments, last by Calcious 9 years, 4 months ago

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")
else:
    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?

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.

Advertisement
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/

A nice convenient tool to help build and test regular expressions is Regexer.

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")

A good and fun place to practice is this one:

http://regexcrossword.com/

Basically a crossword using regular expressions.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Advertisement

Oh I see. thanks.

This topic is closed to new replies.

Advertisement