-1

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?

CosmicCat
  • 612
  • 9
  • 19

2 Answers2

0

You may want to use the strip method which removes whitespace from the beginning and the end of the string.

Change

foodChoice = foodChoice.lower()

To

foodChoice = foodChoice.lower().strip()

More info can be found in this answer.

godsuya
  • 1
  • 3
0

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.

Diogo Rocha
  • 9,759
  • 4
  • 48
  • 52