简体   繁体   中英

random selected dictionary values checked and restarted if matched

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM