0

I´m making a vote system, and i´m making a input, where´s the "user" put the name of the candidate and the name is registrered, but when the "user" ( a.k.a me ) press "0" to exit, they show the list of candidates normally, but the letters show separated and with the comma, like "T, h, o, m, a, s" or "P, e, t, e, r".

I´ve tried making it string, and using the "join" function, but it aplicates the ", " for space in every letter.

def urna():
    candidatos = []
    completo = False
    while completo == False:
        adicionar = input("Coloque seus candidatos aqui, ( 0 para sair ): ")
        if adicionar == "0":
            candidatos_formatados = " ".join(candidatos)
            print()
            print("Fim!")
            print()
            print("Esse foi(ram) o(s) candidato(s) registrado(s):", candidatos_formatados)
            break 

        else:
            print()
            candidatos.extend(adicionar)
            print("Foi adicionado(a) o(a) candidato(a) %s" % adicionar)
            print()

I expect the output like: "Thomas, Peter, Johann, Hawking, Stephen" and so on; But the actual output of this print is: "T, h, o, m, a, s", look at the image below:

My printscreen of the issue

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

2 Answers2

0

You need to append not extend:

k = []

k.append("Tom")   # k == ["Tom"]

k.extend("Apple") # K == ["Tom", "A", "p", "p", "l", "e"]

See Difference between append vs. extend list methods in Python and more on lists

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-1

extend(): Iterates over the arguments and add each element to the list.

l = ['hello']
l.extend('world')
l = ['hello', 'w', 'o', 'r', 'l', 'd']

append(): Adds its argument as a single element to the end of a list.

l = ['hello']
l.append('world')
l = ['hello', 'world']

You need to use append().

Djib2011
  • 6,874
  • 5
  • 36
  • 41