2
import random
names = ['Gio', 'Dano', 'Gian']
chores = [ 'Dookies', 'Restroom', 'Floor']
name = random.choice (names)
chore = random.choice (chores)
phrase = name + '' + chore +''
print (phrase)

I want this to be a random selection to end up like Gio Dookies, Dano Floor, Gian Restroom randomly randomly randomly etc. Thank you

khelwood
  • 55,782
  • 14
  • 81
  • 108
Gianito
  • 21
  • 1

2 Answers2

1

Use random.shuffle to shuffle the list of chores (no need to shuffle the names). Then, for each index in the names list, print the name at this index together with the chore at the index.

When your requirements are more complex (size of names and chores doesn't match, pairings of name/chore shouldn't repeat etc.) you need a more complex algorithm, of course.

  • If possible could you give me an example of how you would do it. I am not sure what an index is because I haven't learned about it yet. – Gianito Oct 11 '20 at 07:58
  • Have a look at https://stackoverflow.com/q/1663807/6387948 to see how you can iterate through parallel arrays. But if you're at a really novice level with Python programming, going through tutorials may be more effective than asking people to tell you solutions to individual problems. – Hans-Martin Mosner Oct 11 '20 at 08:08
1

You can first shuffle the list of chores to ensure the order is random, and then pop elements from this list one by one (therefore returning them, while also dropping them from the list reference). For example:

import random
names = ['Gio', 'Dano', 'Gian']
chores = [ 'Dookies', 'Restroom', 'Floor']
random.shuffle(chores) # this shuffles inplace, so no need to reassign
for name in names:
    chore = chores.pop() # returns the last element of the chores
    phrase = name + ' ' + chore +' '
    print (phrase)

This will effectively assign every one of your names (people) to a chore in the list of chores. It's useful because you can also have duplicate chores (for example you need 2 people to do the "Floor"), and that can be reflected in your list from which you pop().

tania
  • 2,104
  • 10
  • 18