所以我正在尝试通过 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 循环上。我想创建一个函数,它会一直运行直到所有三个键都不同。最后,它将打印结果。有任何想法吗?提前致谢。