How do I make Python not turn to the upper or lower case letters of the code?

Asked

Viewed 104 times

0

Code:

while(1):
    x= str (input ('Meu avô tinha dois cachorros, um se chamava Pet e o outro se chamava Repete, o Pet morreu, quem é que ficou?'))
    if x != 'Repete':
        print("Errado!")
        break

The idea of the code is that boring old joke of the 5°year, that is, every time someone type "Repeats", the program will keep repeating, the problem is that if the user writes "repeats" instead of "Repeats", the code breaks, I have no idea how to solve it.

  • 1

    Use the methods str.lower() or str.upper() to get a copy of enter in lowercase or uppercase and make the comparison: if x.upper() != 'REPETE':

  • And here x= str (input ('Meu avô... that str is unnecessary input already returns a string.

3 answers

4

As already stated in the comments:

Use the methods str.lower() or str.upper() to get a copy of the entry in lower or upper case and make the comparison.

Apply a conversion of str() the exit of a input() is unnecessary as input return is already a string.

In the example the expression x:= input(pergunta).upper() != 'REPETE' can be understood as such:

  1. Is assigned input(pergunta).upper() to x by means of a expression of assignment(Walrus operator) x:= input(pergunta).upper().
  2. Comparison shall be made whether x != 'REPETE'.

The result of this expression is tested every iteration of while.

pergunta = 'Meu avô tinha dois cachorros.\nUm se chamava Pet e o outro se chamava Repete.\nO Pet morreu, quem é que ficou?\n'

while(x:= input(pergunta).upper() != 'REPETE'):
  print("Errado!")
print("Você acertou!")

Test the code on Repl.it

1


Note that this program, just as the author himself said, is a bad joke.

The purpose of this program is, if the user enters the answer CERTAIN the programme is re-dispatched. And, if the response is WRONG the program displays an error message and terminates.

The idea of the code is that boring old joke of the 5°year, that is, every time someone type "Repeat", the program will keep repeating...

According to this part of the statement we must realize that the program ALWAYS repeat if the word typed is repete, regardless of whether their characters are uppercase or lowercase or mixed.

One of the ways to resolve this issue is by using expression of assignment. That way we can:

  1. Capture the answer and convert all your characters to uppercase;
  2. Check whether this answer, in fact, is REPETE;
  3. Re-linking or closing the programme.

One way we can implement code is:

def fato():
    return 'Meu avô tinha dois cachorros. Um se chamava Pet e o outro ' \
           'se chamava Repete. O Pet morreu.'


while (n := input(f'{fato()} \nQuem é que ficou? ').upper()) == 'REPETE':
    '\t'
print("ERRADO!")

Well, as long as the user’s answer is the right answer according to the question, the program will be re-forwarded. And, if the answer is WRONG according to the question, the program will display an error message and automatically terminate.

Testing the code:

When executing the program, while typing the characters belonging to the word repeats, in the order in which they make sense, regardless of whether they are uppercase or lowercase, the program will be re-run. Otherwise, the program will show us the message ERRADO and close the execution.


Now if you don’t want to use the attribution axpressure, you can implement the following code:

while 1:
    x = input('Meu avô tinha dois cachorros, um se chamava Pet e o outro se chamava Repete. '
              'O Pet morreu. \nQuem é que ficou? ').capitalize()
    if x != 'Repete':
        print("Errado!")
        break

Note that this code is the corrected form of the code presented in the question.

First correction:

The parentheses that surround the value 1 are unnecessary. That is why.

Second correction:

The function str() is unnecessary, since the return of the function input() will always be a string. That’s why I removed it.

Third correction:

If you want to compare the answer with the string Repeats, you need to convert the string typed with the function capitalize. Therefore, I inserted this function at the end of the function input().

Fourth correction:

The main function of the program is to repeat the question if the user’s answer is correct and otherwise displays the message Wrong! and CLOSES program execution. It is therefore unnecessary to display messages Got it right! or You got it right!.

  • 1

    Thank you very much, explained in a very clear way to me.

1

One way to solve this issue by keeping your code would be as follows, using the function capitalize()

while(1):
    x= input('Meu avô tinha dois cachorros, um se chamava Pet e o outro se chamava Repete, o Pet morreu, quem é que ficou?')
    if x.capitalize() != 'Repete':
        print("Errado!")
        break
    else:
        print("acertou")
  • 1

    I hadn’t noticed that, thanks for the remark!

  • 1

    kk Ta ok buddy, you’re cleared to go there now!

Browser other questions tagged

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