Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99908

Re: HELP PLEASE printing single characters!

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: HELP PLEASE printing single characters!
Date 2015-12-02 13:08 -0600
Message-ID <mailman.143.1449083343.14615.python-list@python.org> (permalink)
References <75854ef5-fdd5-49da-88e8-27687b8d31c6@googlegroups.com>

Show all headers | View raw


On Wed, Dec 2, 2015 at 12:58 PM, Dylan Riley <dylan.riley@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

HELP PLEASE printing single characters! Dylan Riley <dylan.riley@hotmail.com> - 2015-12-02 10:58 -0800
  Re: HELP PLEASE printing single characters! Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 13:08 -0600
    Re: HELP PLEASE printing single characters! Dylan Riley <dylan.riley@hotmail.com> - 2015-12-02 11:44 -0800
      Re: HELP PLEASE printing single characters! Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 14:15 -0600
  Re: HELP PLEASE printing single characters! John Strick <jstrickler@gmail.com> - 2015-12-02 16:08 -0800
    Re: HELP PLEASE printing single characters! Larry Hudson <orgnut@yahoo.com> - 2015-12-03 20:08 -0800

csiph-web