-5

I have this Python program to play the game Celebrity ID and in my game, I have 15 rounds.

The code:

while round<15
         import random
         celeblist=["a","b","c","d","e","f"] ##and so on
         celebchoice=random.choice(celeblist)
         celeblist.remove(celebchoice)

but it is not working and I would like to know how I can delete the item from the list permanently so it is deleted for the 15 rounds.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58

2 Answers2

1

Currently, you recreate the list at each iteration of the loop. You need to create the list before the loop. Also:

  • prefer to make a for loop with a range (which is an iterator in Python 3) instead of a while
  • Import random at the beginning and not in the loop

The code corrected:

import random
celeblist = ["a","b","c","d","e","f"]  # and so on

for round in range(15):
     celebchoice = random.choice(celeblist)
     print("Current elem: %s" % celebchoice)
     celeblist.remove(celebchoice)
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
0

Why not preselect your random celebrities, one per round?

import random

celebs = [
    "a", "b", "c", "d", "e", "f",
    "g", "h", "i", "j", "k", "l",
    "m", "n", "o", "p", "q", "r"    # need at least 15
]

chosen = random.sample(celebs, 15)
for round,celeb in enumerate(chosen, 1):
    print("{}: {}".format(round, celeb))

which gives

1: j
2: a
3: r
4: f
5: n
6: o
7: g
8: k
9: i
10: l
11: e
12: b
13: d
14: q
15: p
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99