1

For example, I have this mini 'Hunger Games', but sometimes it gets results like "Katniss kills Katniss". How do I stop that?

import random

part = ("Peeta", "Katniss", "Patrick", "Edward Snowden")

p1 = random.choice(part)
p2 = random.choice(part)
p3 = random.choice(part)
p4 = random.choice(part)

acon = ("kills", "kisses", " stalks ")

happ = p1 + ' ' + random.choice(acon)

print happ
shm
  • 29
  • 1
  • 7
  • 1
    `random.shufle(part)` and you get list with elements in random order - and then you can get element one-by-one – furas Jan 23 '17 at 20:50
  • http://stackoverflow.com/q/32368129/139010 – Matt Ball Jan 23 '17 at 20:52
  • @vaultah the question isn't an exact duplicate, has useful answers not included in your link, please reopen – f5r5e5d Jan 23 '17 at 21:49
  • @f5r5e5d: 1) Correct, it doesn't have to be. 2) The only (not very useful) answer was posted after the question was closed. 3) I don't think this question should be reopened, but you are free to flag to reopen anyway. That said, the code in question simply can't output *"Katniss kills Katniss"*, so the lack of [mcve] is another reason it should stay closed. – vaultah Jan 23 '17 at 21:56

1 Answers1

0

You can simply do:

part2 = part[:] #This will create a copy
part2.remove(p1)
happ = p1 + ' ' + random.choice(acon) + ' ' + random.choice(part2)

This will remove the already chosen name p1 from the parts list. The original is not being affected. The modified list is part2.

halfer
  • 19,824
  • 17
  • 99
  • 186
fameman
  • 3,451
  • 1
  • 19
  • 31
  • `part.remove` will remove `p1` from `part` *permanently*. – vaultah Jan 23 '17 at 20:56
  • Oh. My fault. Sorry. I edited my question. Could you please upvote it now? I only ask because just the current answer should be voted and not old ones. Happy Coding ;-) – fameman Jan 23 '17 at 20:59
  • `part2.remove(p1)` this removes from `part` too. You have to create copy `part2 = part[:]` – furas Jan 23 '17 at 20:59
  • `part2 = part` doesn't create a copy of `part`. *"just the current answer should be voted and not old ones"* - uhh, why? – vaultah Jan 23 '17 at 20:59
  • Ok. I edited my anser again. Thank you for your help. Only the current answer should be voted because visitors who are searching for the best answer may not beleive in this answer because it is downvoted But if the answer is now correct you should upvote it to show that it isn't bad anymore. – fameman Jan 23 '17 at 21:04