2012-08-18 2 views
0

앞서 언급했듯이 객관식 퀴즈를 만들기 위해 노력하고 있습니다. 퀴즈는 3 개의 키를 무작위로 "답변"으로 사용합니다. 그런 다음 퀴즈는 3 개의 선택된 키의 값을 받아 "질문"으로 사용합니다. 임의로 선택한 키 값에서 값을 선택하려면 인수로 random.sample 활용하려고합니다. 내 코드는 다음과 같습니다.파이썬 사전에서 무작위 키를 선택하여 무작위 값을 선택하십시오.

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.'} 


def nodupchoice(): 
    key1, key2, key3 = random.sample(word_drills, 3) 
    print "%s, %s, %s" % (key1, key2, key3) 
    #print word_drills.random.sample[key1, key2, key3] 
    #print word_drills[key1] 
    #print word_drills[random.sample(key1, key2, key3)] 


nodupchoice() 

나는 실제로 내가 원하는 결과를 줄 것이라고 생각하는 것을 보았습니다. 불행히도, 각각은하지 않았다. 지침에 대한 도움을 주시면 감사하겠습니다. 다시 한번 감사드립니다.

UPDATE 그래서 내가 제공하는 새로운 정보를 통합했다. 다시 감사합니다.

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.'} 


def nodupchoice(): 
    # For loop that creates a list named keys. It grabs 3 random keys from the dictionary word_drills 
    keys = [x for x in random.sample(word_drills, 3)] 
    # User is presented with a question. A value from the previous randomly selected keys is selected as the 'question' 
    print "Question: ", word_drills[random.choice(keys)] 
    # Set the variables key1, key2, & key3 to the 3 keys in the list 'keys' 
    key1, key2, key3 = keys[0], keys[1], keys[2] 
    # User is presented with 3 choices. 
    print "\n\n(a)%s (b)%s (c)%s" % (key1, key2, key3) 
    selection = raw_input("> ") 
    print selection 

nodupchoice() 

나는 것이 지금 해독하기 위해 노력하고있어 문제 : 사전 word_drills에 무엇인지에 대한 사용자의 선택을 확인하는 방법이 내가 생각 해낸 것입니다. if/else를 사용할 계획이었습니다. 그것이 틀렸다면, 그것은 당신에게 알리 겠지만 그렇지 않다면 당신은 틀렸다. 그러나 이것에 접근하는 방법을 모릅니다. 도움을 주신 모든 분들께 다시 한번 감사 드리고 싶습니다.

print word_drills[random.choice([key1, key2, key3])] 

이 무작위로 샘플링 키 중 하나에 속하는 값 중 하나를 인쇄합니다 :

답변

1

당신은 키를 선택 random.choice()를 사용하여 시도 할 수 있습니다 :

keys = random.sample(word_drills, 3) 
print keys 
print word_drills[random.choice(keys)] 

출력을 :

['has-a', 'attribute', 'class'] 
A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth. 
+1

여기서 listcomp이 어떤 역할을하는지 이해하지 못합니다. 'lst'는 항상'random.sample (word_drills, 3)'와 같지 않습니까? – DSM

+0

예, 가능합니다. 이 경우에는 random.choice()를 사용하고 선택된 모든 키를 더 쉽게 인쇄 할 수 있도록하는 것이 좋습니다. –

+1

나는 DMS가 의미하는 바를 추측한다 :'random.sample'은 이미 list/iterable을 반환하기 때문에 다시 래핑 할 필요가 없다. –

2

나는 당신이하고 싶었던 것은 이것이다 같아요.

+0

나는 프로그램이 무작위로 키 1, 키 2, KEY3의 값 중 하나를 선택합니다. 그렇게하면 값 (또는 질문)이 표시 될 때이를 보장 할 수 있습니다. 해당 키 (또는 대답)가 이미 가능한 선택으로 나타났습니다. – kiddsupreme

+0

그럴 수는 있겠지만'key3] 뒤에 닫는 괄호가 없을 수도 있습니다. –

+0

예. 감사합니다. –