Game of chance project

At the end of the challenge, we are asked to smoothen edges in the code so that it’s hard to break.

they say, what if user inputs “head” or “heads !” or “HeAD” etc

my question is: is there a like operator in python similar to one in SQL?
or how to solve this without one.

Sounds like something you might want to do some research on? I think that is a good learning opportunity

here are some spoilers to help you if you really can’t get any further:

in keyword
.lower() method

1 Like

hi! yes, i did do my homework.

i am aware of building a list of all possibilities and then using ‘in’ operator.
i tried to even convert the ‘guess’ into lower alphabets using .lower()
and then again used list and ‘in.’

my code is better than before, i am just looking for better solutions. or a way to use the LIKE operator of SQL in python

What is it LIKE would do for you that you’re not already doing? If you make a decision on what behavior you want then you’re a whole lot closer to having it than if you’re looking for a “solution” which is too vague to be useful.
If you’re looking for pattern matching then that’s something regular expressions can do for you. Or, if you normalize the data so that all equivalent things converge into one value, then you put yourself in a position where you can test whether two things are equal.

Can you give me some clue on regular expressions?
and how can i normalise the entire user input and how to do pattern matching?

If you think two things are the same, then, if you put into words what’s the same about them, then that’s something you can extract from both which would give you a normalized version.

So it’s less about finding something that does what you want and more about deciding what you want.

If what you think is the same is the letters (now I’ve put into words what it is, so now it is something that can be carried out), then grab the letters from both, they’ll match.

Don’t know what you mean by clue on regular expressions.

@appylpye @mtf

Hi guys! can you help me with this problem? I believe my question hasn’t been understood.

All I am asking is that regardless of whatever the user types in as input i.e. “heads” or “HEADS!!” or “HeadSS !!” etc my code reads the “head” part of it and runs smoothly.

i.e. like in SQL, if i type in : where user_input like ‘head%’

i.e. all values beginning with ‘head’ regardless of whatever lies behind it.

the in operator isn’t only for lists, we can also use in to determine if sub-string in the string:

print("bc" in "abcd")

building a list with all the options would be tedious and repetitive, and you can’t possible predict all outcomes.

1 Like

Consider this example:

response = input("heads or tails? ")
if response.lower() in ["heads", "tails"]:
  print("Nice input!")
else:
  print("Not nice input!")

Sample runs:

heads or tails? heads
Nice input!

heads or tails? tails
Nice input!

heads or tails? HeAdS
Nice input!

heads or tails? ears
Not nice input!

Edited on January 2, 2020 to add the following example:

Also consider this as a condition:

if "head" in response.lower():

It conforms to what was suggested here:

You’re asking either for something very specific that happens to do what you’re looking for, or something so incredibly powerful and magical that it does pretty much anything.

The first option is unlikely, and you haven’t defined what it would be anyway.
The second option doesn’t belong in a programming language. That’s like replacing the English language with one word or a grunt to mean anything, in making it mean everything it loses meaning, there isn’t enough information in it.

But since python is a programming language, you could decide what that entails and then implement it.

That, in addition to ignoring case, would be something you can implement. It’s not going to be one operator, but it’s also not going to be a lot of code, or difficult.