Python Forum
Using random.choice v.v
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using random.choice v.v
#1
I'm trying to create a sentence with random words from multiple different lists.
such as
lista = blah, blah,
listb = blah, blah,
listc = blah, blah,

i want the output to be "list a random choice, list b random choice, list c random choice"
Any help on the random.choice command? i'm new at programming and I want to get a good grade.

Thank you
Reply
#2
I did not read the Homework rules until just now, sorry.
################current code###############
old_people = ['George', 'Bob', 'Owen']
young_people = ['Sarah', 'Gwen', 'Brittney']
mid_age = ['Larry', 'Missy', 'Greg']


import random
print(random.choice(old_people))
 
################
this is my current code and it works great. but it only produces one word like
"george"

I want it to produce in sentence form. for example

outcome: "George Sarah Missy"
or
outcome: "Bob Sarah Larry"
Etc
They are random from each list and each list is in order.

Ive tried ->
print(random.choice(old_people, mid_age)) 

just to try two together, but it wont work..

how would I pick 1 name from all three lists in sequence on the same line?
Reply
#3
You should repeat random.choice on all lists, not put all list into one random.choice. Something along those lines:

>>> old_people = ['George', 'Bob', 'Owen']
>>> young_people = ['Sarah', 'Gwen', 'Brittney']
>>> mid_age = ['Larry', 'Missy', 'Greg']
>>> ' '.join([random.choice(old_people), random.choice(young_people), random.choice(mid_age)])
'Bob Brittney Larry'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
You can use map, e.g.

import random
list(map(random.choice, (lista, listb, listc)))
Reply
#5
(Aug-31-2019, 03:58 AM)perfringo Wrote: You should repeat random.choice on all lists, not put all list into one random.choice. Something along those lines:

>>> old_people = ['George', 'Bob', 'Owen']
>>> young_people = ['Sarah', 'Gwen', 'Brittney']
>>> mid_age = ['Larry', 'Missy', 'Greg']
>>> ' '.join([random.choice(old_people), random.choice(young_people), random.choice(mid_age)])
'Bob Brittney Larry'

THANK YOU perfringo!!

Your code wasnt exactly what I was looking for, BUT it did lead me into the right direction.

I know have this code

 old_people = ['George', 'Bob', 'Owen']
young_people = ['Sarah', 'Gwen', 'Brittney']
mid_age = ['Larry', 'Missy', 'Greg']


import random
print([random.choice(old_people), random.choice(young_people), random.choice(mid_age)])    


The only issue now is that it prints ['Owen', 'Gwen', 'Greg']
I just want it to print without commas, brackets, or quotation marks.

What would I need to add to my code to accomplish this?
Reply
#6
(Aug-31-2019, 04:30 AM)Dawkinz Wrote: What would I need to add to my code to accomplish this?

Just one * for unpacking:

>>> print(*[random.choice(old_people), random.choice(young_people), random.choice(mid_age)])
George Sarah Larry
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Thank you perfringo!

Thanks to your help I was able to get my commands to work the way I wanted them to.
I really appreciate your reply.
Reply
#8
(Aug-31-2019, 04:30 AM)Dawkinz Wrote:
print([random.choice(old_people), random.choice(young_people), random.choice(mid_age)])

The only issue now is that it prints ['Owen', 'Gwen', 'Greg']
I just want it to print without commas, brackets, or quotation marks.
What would I need to add to my code to accomplish this?
I would just not put all random choices into a list (don´t use the [] brackets)
print(random.choice(old_people), random.choice(young_people), random.choice(mid_age))
Reply
#9
(Aug-31-2019, 10:55 AM)ThomasL Wrote:
(Aug-31-2019, 04:30 AM)Dawkinz Wrote:
print([random.choice(old_people), random.choice(young_people), random.choice(mid_age)])

The only issue now is that it prints ['Owen', 'Gwen', 'Greg']
I just want it to print without commas, brackets, or quotation marks.
What would I need to add to my code to accomplish this?
I would just not put all random choices into a list (don´t use the [] brackets)
print(random.choice(old_people), random.choice(young_people), random.choice(mid_age))

WOW!! ThomasL! Thank you! That is exactly what I was looking for! Much cleaner code!

We havent learned the asterisk usage yet, so it's going to look way better without it in my code.

MUCH cleaner code, tahnk you! Wow awesome!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assistance with pythons random.choice coleslaw 5 2,590 Nov-19-2019, 12:01 AM
Last Post: ichabod801
  Just some second choice answers help please ajaY 6 5,082 Apr-07-2017, 08:25 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020