Lukas Johmann911 Points
Purpose of the ".lower" function
Can someone explain what exactly the purpose of the ".lower" function is. Which ability does it give the input in our code?
for example here:
should_proceed = input("Do you want to proceed? Y/N ")
if should_proceed.lower == "y":
Ivan Kazakov43,310 Points
Braces after the
.lower should indeed be used in this context, like this:
.lower(). Otherwise the expression is evaluating whether the
.lower method of
str type equals to
Since Python is case-sensitive, using
.lower() would allow to evaluate that expression to
True for both
y. And to
False for everything else.
Travis Whiteman3,342 Points
An example of this could be used in a rock-paper-scissor game. In my python class at my college, we had to create a command line rock-paper-scissor game. So in my if statement I had the following code:
if player.lower() == computer: print("Tie\n") elif player.lower() == "scissor": if computer.lower() == "lizard": youWin() elif computer.lower() == "paper": youWin() else: youLose()
In this case, the .lower() would ensure that whatever the player input to the command prompt, it would always be a lowercase. So if I did PapEr the computer program would read it as paper. or if I did papeR, the computer would see it as paper, and so on.