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


Groups > comp.lang.python > #99907 > unrolled thread

HELP PLEASE printing single characters!

Started byDylan Riley <dylan.riley@hotmail.com>
First post2015-12-02 10:58 -0800
Last post2015-12-03 20:08 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#99907 — HELP PLEASE printing single characters!

FromDylan Riley <dylan.riley@hotmail.com>
Date2015-12-02 10:58 -0800
SubjectHELP PLEASE printing single characters!
Message-ID<75854ef5-fdd5-49da-88e8-27687b8d31c6@googlegroups.com>
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"].

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
    while choice in LIST:
        LIST.remove(choice)
print(order)
        
    
    
input("press enter to exit")

thanks in advance guys

[toc] | [next] | [standalone]


#99908

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-02 13:08 -0600
Message-ID<mailman.143.1449083343.14615.python-list@python.org>
In reply to#99907
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.

[toc] | [prev] | [next] | [standalone]


#99909

FromDylan Riley <dylan.riley@hotmail.com>
Date2015-12-02 11:44 -0800
Message-ID<39698cab-6f54-4a27-8626-9625dbaf6917@googlegroups.com>
In reply to#99908
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@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.

[toc] | [prev] | [next] | [standalone]


#99911

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-02 14:15 -0600
Message-ID<mailman.145.1449087377.14615.python-list@python.org>
In reply to#99909
On Wed, Dec 2, 2015 at 1:44 PM, Dylan Riley <dylan.riley@hotmail.com> wrote:
> 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.

order.append(choice)

[toc] | [prev] | [next] | [standalone]


#99923

FromJohn Strick <jstrickler@gmail.com>
Date2015-12-02 16:08 -0800
Message-ID<e347b491-ba2a-41fc-a180-741ddf53c22c@googlegroups.com>
In reply to#99907
On Wednesday, December 2, 2015 at 12:58:30 PM UTC-6, Dylan Riley 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"].
> 
> 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
>     while choice in LIST:
>         LIST.remove(choice)
> print(order)
>         
>     
>     
> input("press enter to exit")
> 
> thanks in advance guys

You could just shuffle the list first, then loop through it. This will guarantee that each color is only used once. 

    import random
    LIST = ["blue ", "red ", "yellow ", "green ", "orange "]
    random.shuffle(LIST)
    for color in LIST:
        print(color)
        # or add to order or whatever you need to

   

[toc] | [prev] | [next] | [standalone]


#99993

FromLarry Hudson <orgnut@yahoo.com>
Date2015-12-03 20:08 -0800
Message-ID<5sednRMaaYkrjPzLnZ2dnUU7-bWdnZ2d@giganews.com>
In reply to#99923
On 12/02/2015 04:08 PM, John Strick wrote:
> On Wednesday, December 2, 2015 at 12:58:30 PM UTC-6, Dylan Riley 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"].
>>
>> 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
>>      while choice in LIST:
>>          LIST.remove(choice)
>> print(order)
>>
>>
>>
>> input("press enter to exit")
>>
>> thanks in advance guys
>
> You could just shuffle the list first, then loop through it. This will guarantee that each color is only used once.
>
Not quite.  Only if the original list has no repetitions.

My personal approach would be to use a set to eliminate the duplicates, convert back to a list 
and shuffle that.

no_reps = list(set(LIST))
random.shuffle(no_reps)
print(no_reps)   #  Or use loop to print one-per-line

      -=- Larry -=-

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web