HELP PLEASE printing single characters!

Dylan Riley dylan.riley at hotmail.com
Wed Dec 2 14:44:38 EST 2015


On Wednesday, December 2, 2015 at 7:09:23 PM UTC, Ian wrote:
> On Wed, Dec 2, 2015 at 12:58 PM, Dylan Riley <dylan.riley at hotmail.com> wrote:
> > hi all,
> > I have been trying to figure out all day why my code is printing single characters from my list when i print random elements using random.choice the elements in the list are not single characters for example when i print, print(LIST[random.choice]) i get:
> > ["e", "x", "a", "m", "p", "l", "e"] when i should get ["example"].
> 
> Remember that strings are iterable, and that iterating over strings
> results in individual characters. That should give you a clue as to
> what's going on.
> 
> > my code is:
> > #Create a program that prints a list of words in random order.
> > #The program should print all the words and not repeat any.
> >
> > import random
> >
> > LIST = ["blue ", "red ", "yellow ", "green ", "orange "]
> > order = []
> >
> > print("This game will print a random order of colours")
> > print("The list is", LIST)
> > input("press enter to start")
> >
> >
> >
> > while LIST != []:
> >     choice = random.choice(LIST)
> >     order += choice
> 
> Addition on a list does concatenation, not appending. So this takes
> each element from choice and adds them individually to order.

hi ian what would be the correct code to use in this situation then because as far as i am aware the elements of my list should be printed as whole elements and not just characters of the elements.



More information about the Python-list mailing list