Ignore if it is uppercase and lowercase in the string

Asked

Viewed 3,324 times

1

n1 = input('Qual é o seu bolo favorito?')

if n1 = 'Bolo de Chocolate':
   print('teste123')

If the user writes "Chocolate Cake" all in minuscule or all in uppercase, it will happen not to enter the if that I created, does anyone know if it has any function that ignores if it is uppercase or lowercase?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

5

You can use the lower() to make everything tiny. Whenever you have a question if you have a function to do something, you can look in the official documentation about the type of data you are working on. Would look like this:

n1 = input('Qual é o seu bolo favorito?')
if n1.lower() == 'bolo de chocolate':
    print('teste123')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Could use the upper() also. And of course it doesn’t solve all the other problems of the person typing different, which is much more likely to give some problem.

  • it would be good to put both sides of the check with Lower() or upper() example: if n1.Lower() == 'chocolate cake'. Lower()

  • @le314u why?

3

You have to compare both versions in lower case (or upper case). The function .lower() of a string transforms all characters into its lowercase versions:

n1 = input('Qual é o seu bolo favorito?')

if n1.lower() == 'bolo de chocolate':
   print('teste123')
  • it would be good to put both sides of the check with Lower() or upper() example: if n1.upper() == 'chocolate cake'. upper()

-3

To solve your problem once and for all, instead of giving the user the possibility to write (since it can happen to type wrong and in this case will never enter the if) you can offer a list of answers for him to select one of them. type using Autocompleteedittext in its layout instead of Edittext.

  • nice your Autocompleteedittext reasoning but what if the person does not want the answer to be explicit... suppose you are working out a puzzle game

Browser other questions tagged

You are not signed in. Login or sign up in order to post.