Извлекать случайные ключи из словаря в Python, которые не равны друг другу

Итак, я пытаюсь настроить викторину с несколькими вариантами ответов через Python. Я новичок в Python, поэтому заранее извиняюсь, если есть более простой способ сделать это. Тем не менее, я пытаюсь действительно понять некоторые основы, прежде чем переходить к более новым методам.

У меня есть словарь. В этом словаре я хочу получить 3 случайных ключа. Я также хочу убедиться, что эти три ключа не равны (другими словами, случайны друг от друга ). Вот код, который я написал до сих пор:

import random

word_drills = {'class': 'Tell Python to make a new kind of thing.',
               'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
               'instance': 'What you get when you tell Python to create a class.',
               'def': 'How you define a function inside a class.',
               'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
               'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
               'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
               'attribute': 'A property classes have that are from composition and are usually variables.',
               'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
               'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}

key1 = ' '
key2 = ' '
key3 = ' '             

def nodupchoice():
    while key1 == key2 == key3: 
        key1 = random.choice(word_drills.keys())
        key2 = random.choice(word_drills.keys())
        key3 = random.choice(word_drills.keys())


nodupchoice()

print "Key #1: %s, Key #2: %s, Key #3: %s" % (key1, key2, key3)

Я почти уверен, что проблема связана с моим циклом while. Я хотел создать функцию, которая будет работать до тех пор, пока все три клавиши не будут отличаться друг от друга. Наконец, он напечатает результат. Любые идеи? Заранее спасибо.

6
задан kiddsupreme 18 August 2012 в 21:02
поделиться