Well, there are some syntax errors in your code. I will try to list them.
1º - To declare a variable of type string just do:
nomeVariavel = "conteúdo da string"
2º - Your function is not receiving any parameters. The function should receive parameters, which will be the values used within the function.
Ex.:
def quantosIguais(a, b):
3º - The variable and, which will be used as a counter, must be declared within the scope of the function.
4º - To find out the size of the string, it is made len(nomeString)
and not (len)nomeString
.
5th - In the ties for that you used, they only go through 1 value, which is the letter you placed. To use the variable, just put the variable name after the in
. Using quotation marks, you traverse a new string, unlike the string stored in the variable.
Do:
for a in nomeString
instead of:
for a in 'nomeString'
Well, I tried to list the syntax errors, but like I said, your code doesn’t make sense. I’d also like to understand what your logic was for getting to this code.
I’ll leave an example of a function that does what you tried to do:
def quantasIguais(strA, strB):
e = 0
for i in range(len(strA)):
if strA[i] == strB[i]:
e += 1
return e
Can you describe your code line by line? I can tell you that it does not make sense, but I prefer to see what was your reasoning when drafting it. Maybe you’d better start studying algorithm building before venturing into a programming language.
– Woss
I thought the following: in the first line I wanted the function to be executed while the value of e was less than the length of A, then I wanted to search inside B, values that resemble A and if this function was true add 1 to and.
– Victor Boscaro
you know that when you assign a variable with
=
the new variable is on the left side, isn’t it? The sign of=
in Python and in almost no language is not like in mathematics where he "defines" that two things are equal - he assigned to the left name the result of the right side expression.– jsbueno