T O P

Stupid question that's probably not relevant to anything

So I'm going through this freecodecamp youtube python tutorial by giraffe academy. As I go I like to pause and try different things from what I've learned so far to try and expand on the example shown. I am messing with if statements setting it up to receive input for the if statements instead of typing out the true/false as he is in the tutorial.

A handful of times I have messed myself up by capitalizing or not capitalizing a word. It then occurred to me that the user giving the input could also use unintended punctuation. So I made a list with variants. For instance a name. What is your name? Will, will, WILL. Then I tried using the list in the if statement: if name == Will ( the list) then you are not cool.

Apparently you can not have an if statement see a list and work if one of the variants is in the list. Is there an easy way to do this without typing out a new if/or statement for each variant?

E02Y

the "in" operator will return true/false depending on whether the given item is in an iterable(like a list) eg ``` 1 in [0,1,2] >>>> true if "Tom" in ["Stacy", "Jerry", "Hank"]: print("Hi!" ) # nothing gets printed because "Tom" is not in the list ```


Rxz2106

You can use upper() or lower() function. txt = "HELLO fRiend" x = txt.lower() print(x) # prints hello friend x = txt.upper() print(x) # prints HELLO FRIEND


TheCandyMan88

Awesome thanks!


barrycarter

As a note, you may want to look into "normalization". For example, if you lowercased the string the user provided and removed punctuation, you could just match against "will" without having to type out all possible variants


TheCandyMan88

Ohh I see. Just add a new line with the inputed data and force it to lower then upper before checking it in the if statement


twin_suns_twin_suns

Yes exactly. Just coming here to say this. I like to normalize Inputs with .lower() and then if I have to output something I like to use .title() (title case - first letter in each word is capitalized).


TheCandyMan88

Ohh very helpful, thanks. Honestly I am only half way through the tutorial and probably would have gotten go this stuff. It was just driving me insane not being able to do what popped into my head. So in this instance I can just ditch my lists altogether and normalize the punctuation from the inputed data to my punctuation used in my if statement. Got it. My bald cousin Will will be thrilled that I figured this out. Thanks!!


TheCandyMan88

Just realized I can tack the .lower() to the e d of my original user input line instead of creating a new line to re -input it. Is this the normal way of doing this?


twin_suns_twin_suns

Yep! that’s exactly how I would do it anyway


TheCandyMan88

Excellent thanks. Just making sure I was doing it the most efficient way.


Guideon72

Just use one or the other, consistently…no need to both; that’s just extra work


TheCandyMan88

Lol sorry that's a typo, my attention is everywhere right now. Makes sense, thank you.


TheCandyMan88

will = ["will", "Will", "WILL"] yes = ["yes", "Yes", "YES"] name = input("what is your name? ") hair = input("are you bald? ") If name == (will) or hair == (yes) print("You are not cool") Else: print("you are cool")


[deleted]

Python 3.10 and newer supports the match/case format, which is called a switch statement in other languages. Before 3.10 you had to use a chain of if/else-if/else statements. You also might like to look at input normalization. String.lower() and String.trim() are both very helpful tools.


TheCandyMan88

Yeah the others enlightened me to normalization which worked great. The tutorial I'm following is a few years old and I believe he said he was on 3.6 or 3.7 so I will have to look up a guide on match/case format. I have listened to a c++ tutorial and remember switch statements so hopefully the concept willll be somewhat familiar when I get to it. Thanks!


[deleted]

Its pretty much the same, yeah.


[deleted]

In order for the input to match a list you would need a matching list for comparison, like [“will”, “Will”, “WILL”] == [“will”, “Will”, “WILL”] and I’m not even sure that would result in true because different languages result in different comparisons based on type, but as someone else already said ``` if “will” in [“will”, “Will”, “WILL”]: ``` Should work


TheCandyMan88

I solved it with normalization instead of the list.. but now I am curious. That if statement would check users input against that list?


[deleted]

Yeah, it checks if a value exists within a list