Python - Spaces appear out of thin air when printing print()

Asked

Viewed 121 times

-1

I have a problem with my function, I am very beginner in python so I do not understand much of the language, so I apologize if it is a very amateur mistake. When I print on the screen the result through the print() the first line comes out normal, but every beginning of the second appears with a space at the beginning. Example:

Jose Ganhou o total vendido: 0
Bônus: 0
 Jose Ganhou o número de clientes: 0
Bônus: 0

The second line is always being written with that space before the name, someone can help me?

The code:

def EncontrarOrdem(self,maior,nome,valor,tipo):
    for i in range(0,4):
        if(maior[0] == valor[i] and
            maior[1] != valor[i]):
            bonus = 1500 * 0.4
            return (bonus,nome[i] + ' Ganhou o ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        elif(maior[0] == valor[i] and
            maior[1] == valor[i]):
            bonus = 1500 * 0.7 / 2
            return (bonus,nome[i] + ' Empatou com o segundo lugar no ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        if(maior[1] == valor[i] and
            maior[2] != valor[i]):
            bonus = 1500 * 0.3
            return (bonus,nome[i] + ' ficou em segundo(a) no ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        elif (maior[1] == valor[i] and
              maior[2] == valor[i]):
            bonus = 1500 * 0.5 / 2
            return (bonus,nome[i] + ' Empatou com o terceiro lugar no ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        if(maior[2] == valor[i] and
            maior[3] != valor[i]):
            bonus = 1500 * 0.2
            return (bonus,nome[i] + ' ficou em terceiro(a) no ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        elif (maior[2] == valor[i] and
              maior[3] == valor[i]):
            bonus = 1500 * 0.3 / 2
            return (bonus,nome[i] + ' Empatou com o quarto lugar no ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
        if(maior[3] == valor[i]):
            bonus = 1500 * 0.1
            return (bonus,nome[i] + ' ficou em último(a) no' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n')
  • Strings are starting with a variable, and ending with an n. And I’ve already checked the variables, they don’t contain spaces at first, so all I can think about is n, does it leave any junk in my memory? Note: the two return values are float and string, then I just print the string when calling the function in the code

3 answers

0

Hello, your strings have space at the beginning and at the end. For example: ' car ' Just remove. It would look like this: 'car' I believe that’s the problem.

0

If there are spaces around the names, you could use nome[i].strip() to remove them. I would also recommend using the format instead of concatenating the values with +, this way you will have a greater control of the output and will not need to make conversions to string, for example:
nome[i] + ' Ganhou o ' + tipo + ': ' + str(valor[i]) + '\n' + 'Bônus: ' + str(bonus) + '\n'
could be replaced by:
f"{nome[i].strip()} Ganhou o {tipo}: {valor[i]}\nBônus: {bonus}\n"

  • greatly improved the code, thank you, but the problem still persists

  • Could you provide an image of this output? Could you also provide the print call? I believe it would be easier. An example of the array of names would also be useful.

  • valLiqui[i] = self.Find order(mayLiqui,valName,valLiqui,'total sold') # I store all variables in an array, as they are data from multiple people. print(valLiqui[i]) # I print them on the screen in the same array , the result I edit in the question

  • Here the items are printed with a line break. Ex.: Pedro He won the total type of sales: 40<br/> Bonus: 600.0<br/> <br/> Pedro He won the total type of sales: 40<br/> Bonus: 600.0<br/> Line break that is not appearing on his output. You could try removing all the ' n' from the end of the Returns to see if they are the causes of these spaces.

0


Try to put in charge print() the argument sep=''. Would something like this:

print(EncontrarOrdem(self,maior,nome,valor,tipo),sep='')

The default tab is a space. When placing '' (nothing between quotes), maybe the space is removed.

  • It worked, thank you very much. I will research more on that argument

  • I’m glad I could help.

Browser other questions tagged

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