Posts: 5
Threads: 1
Joined: Aug 2019
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
Posts: 5
Threads: 1
Joined: Aug 2019
Aug-31-2019, 03:52 AM
(This post was last modified: Aug-31-2019, 03:52 AM by Dawkinz.)
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?
Posts: 1,950
Threads: 8
Joined: Jun 2018
Aug-31-2019, 03:58 AM
(This post was last modified: Aug-31-2019, 03:58 AM by perfringo.)
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.
Posts: 817
Threads: 1
Joined: Mar 2018
You can use map, e.g.
import random
list(map(random.choice, (lista, listb, listc)))
Posts: 5
Threads: 1
Joined: Aug 2019
(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?
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
Posts: 5
Threads: 1
Joined: Aug 2019
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.
Posts: 360
Threads: 5
Joined: Jun 2019
(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))
Posts: 5
Threads: 1
Joined: Aug 2019
(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!
|