I need help with this code

Asked

Viewed 55 times

-1

Personal I wrote the following simple code in my python:

nome = str(input("Qual o seu nome?"))
if nome == 'denise':
  print("Nome bonito")
elif nome == 'maria' or nome == 'pedro' or nome =='joao':
  print("Nome popular no brasil")
else:
  print("Seu nome é bem normal")

If I write Denise python prints the last line "Your name is quite normal" and if I write maria, pedro and etc... even so python prints the last line " Your name is quite normal" Can you help me and let me know what the error of this code is? PS: The version of my python is 3.8.2

1 answer

1

The problem is that the Python language differentiates upper case letters from lower case letters, so the string "Denise" is different from "denise" and surely you are writing the other names with capital letters as for example: "Maria", "Joao", "Pedro".

To fix the problem, just make the string returned from input() lower case using the string method lower(), thus:

nome = input("Qual o seu nome?").lower() # Transforma em minúsculo

if nome == 'denise':
    print("Nome bonito")

elif nome == 'maria' or nome == 'pedro' or nome == 'joao':
    print("Nome popular no brasil")

else:
    print("Seu nome é bem normal")

Another important detail to add is that Python not only differentiates upper-case letters from lower-case letters, but also differentiates letters with accented letters without accents.

So the string "joão" (with accent in A) is different from "joao" (without accent in A).


And here’s a hint bonus to close the reply:

The function input() always (unless overwritten) will return a string even if the user input is numeric. So, use the str() in this case is redundant and will increase your code unnecessarily :)

  • So, I did what you said and it still doesn’t work: I reduced the code and wrote just that. name= str(input("Type your name")) if name ="Denise": print("Nice name") and even if I just type this the python doesn’t print, just ask "type your name" however if I change and put the number 1 in place of Denise it prints. Can you tell me why?

  • No you did not do what I said in the reply. Now you check if the name is "Denise" (capital letters), but if you write "denise" Tiny isn’t gonna work. How about copying the answer code into a separate file, testing the code and studying it to understand where you are missing ?

Browser other questions tagged

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