0

So I am creating a text-based Murder Mystery Game for Learn Python the Hard Way... So this is probably really simple but I can't figure it out.

Essentially I am choosing the location of clues, there are 16 options, 4 large locations, and 4 sublocations within each large location. However I don't want any clue to be located at the same location as another clue.

Currently I have it creating a dictionary for each item and subsequent location but it will choose the same locations sometimes. Would love some help thanks!

def determine_items():
    items_needed = {}
    items = ["weapon", "Clue #1", "Clue #2", "Evidence"]
    print(items_needed)
    for i in items:
        hidden_sub = [0.1,0.2,0.3,0.4]
        hidden_super = [1,2,3,4]
        selected_super = random.choice(hidden_super)
        Selected_sub = random.choice(hidden_sub)
        exact =  selected_super + Selected_sub
        if exact == items_needed.values():
             items_needed.clear()
             determine_items()
        else:
            print("No Duplicates?")
        items_needed[i] = exact
  • Create a list of the current used location, and each time remove an item that is already assigned from the list. that way you're sure that each clues is in a different location. – Nash Oct 27 '18 at 17:19
  • @Nash Can you expand on that a little? The way I am understanding you is a separate list maybe adding a line under exact = ... to add each item as it goes in? How would you suggest checking for/removing the matching items? – Cameron Herritt Oct 27 '18 at 17:30
  • you can check if an item is in a list or dictionary with the instruction `in`. Usage: `if element in mylist:` – Daneel R. Oct 27 '18 at 17:32
  • Oh, of course, oops, thank you, I did know that. Is there any similar method for checking in dictionaries? – Cameron Herritt Oct 27 '18 at 17:36
  • i have two ways of doing this, first create a list of all the locations and each time you add an item to the location remove it from the list second you can check if the assigned location is in already using the `in` operator if it is skip and assign another one. – Nash Oct 27 '18 at 17:41
  • Thanks so much for both your help! – Cameron Herritt Oct 27 '18 at 20:19

1 Answers1

0

This is what I have come up with based on your answers

import random
def determine_items():
    items_needed = {}
    items = ["weapon", "Clue #1", "Clue #2", "Evidence"]
    possible_locations = [1.1,1.2,1.3,1.4,2.1,2.2,2.3,2.4,3.1,3.2,3.3,3.4,4.1,4.2,4.3,4.4]
    for i in items:
        exact = random.choice(possible_locations)
        possible_locations.remove(exact)
        items_needed[i] = exact
    print(items_needed)

determine_items()