Search code examples
pythonpython-3.xif-statementlowercase

Having trouble updating python input into lowercase


So I am trying to create a program that takes in a customer choice of food. I want the user to be able to input their choice in capitals so I can then convert it to lowercase and then compare it using an if statement

Name = str(input("What is your Name?"))
Name = ''.join(Name.split())
foodChoice = str(input("Would you like a Burger or Salad?"))
foodChoice = foodChoice.lower()

if foodChoice == 'burger':
    print("true")
else:
    print("false")

Let's assume the customer inputs "BurgER"

The problem I'm experience is that even after I convert "BurgER" into lowercase after the input is declared, the if statement still regards it as false...take a look at my sample output below

What is your name? Jason

Would you like a Burger or Salad? Burger

false

Process finished with exit code 0

EDIT: The problem seemed to be because I added a space before burger out of habit :/, on that note, is there any way to account for this?


Solution

  • Looking at your output, the problem is that you added a whitespace before your input which made the statement evaluates to false.

    To avoid this, I advise you to use the strip function, thus removing the spaces at the beginning and end of the string.

    So, in your case, it would be:

    foodChoice = foodChoice.lower().strip()
    

    Additionally, you could use rstrip() to only remove whitespaces on the right side of the string, and lstrip() to whitespaces on the left side.