Python,随机单词成问题

时间:2015-01-08 04:39:18

标签: python

我有两个清单:

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
             "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
         "pay attention", "sell", "fail", "accept", "allow", "include"]

我使用random.choice从每个列表中选择一个单词。一旦我有了这些话,我就需要将它们打印成一个问题。例如,如果选择了热和弱,它应该打印:“热是冷,因为弱到__?”

我真的需要帮助,我们将非常感谢您的详细步骤。

我的代码:

import random 

wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad", "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"] 
randomword1=random.choice(wordlists1) 
randomword2=random.choice(wordlists2)

8 个答案:

答案 0 :(得分:4)

我可能会做类似

的事情
>>> wpairs = list(zip(wordlists1, wordlists2))
>>> example, question = random.sample(wpairs, 2)
>>> "{} is to {} as {} is to ?".format(example[0], example[1], question[0])
'small is to big as summer is to ?'

首先,我将两个列表组合成一对列表:

>>> wpairs = list(zip(wordlists1, wordlists2))
>>> wpairs
[('hot', 'cold'), ('summer', 'winter'), ('hard', 'soft'), ('dry', 'wet'), ('heavy', 'light'), ('light', 'darkness'), ('weak', 'strong'), ('male', 'female'), ('sad', 'happy'), ('win', 'lose'), ('small', 'big'), ('ignore', 'pay attention'), ('buy', 'sell'), ('succeed', 'fail'), ('reject', 'accept'), ('prevent', 'allow'), ('exclude', 'include')]

然后我会使用random.sample来选择其中两个:

>>> example, question = random.sample(wpairs, 2)
>>> example, question
(('weak', 'strong'), ('heavy', 'light'))

在这里使用random.sample的一个主要优点是你不必担心两次绘制同一对(没有“弱到弱就像弱到?”的问题。)

在此之后,我们可以提出一个问题字符串:

>>> "{} is to {} as {} is to ?".format(example[0], example[1], question[0])
'weak is to strong as heavy is to ?'

答案 1 :(得分:2)

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
                     "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
                 "pay attention", "sell", "fail", "accept", "allow", "include"]

import random

t = zip(wordlists1, wordlists2)
t1, t2 = random.sample(t, 2)
print '%s is to %s as %s is to ___? (%s)' % (t1[0], t1[1], t2[0], t2[1])

Shoould打印出类似的东西 dry is to wet as ignore is to ___? (pay attention)

更新:我从random.sample(t, 2)切换到random.choice。这是更好的方法。 (正如DSM建议的那样,但我也想更新我的代码。)

答案 2 :(得分:1)

您使用了random.choice两次,这使得randomword1在列表中的排名方面与randomword2不同。请改用random.randint来获取统一索引:

import random 
wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad", "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"] 
idx1 = random.randint(0, len(wordlists1)-1)
idx2 = random.randint(0, len(wordlists1)-1)
words_to_choose = (wordlists1[idx1], wordlists2[idx1], wordlists1[idx2], wordlists2[idx2])
print '%s is to %s as %s is to ___? (answer: %s)'%words_to_choose

#OUTPUT: reject is to accept as exclude is to ___? (answer: include)

答案 3 :(得分:1)

import random 
wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad",      "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female",     "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"]
l1=len(wordlists1)
index1=int(random.random()*l1)
index2=int(random.random()*l1)
myquestion=wordlists1[index1]+" is to "+wordlists2[index1]+" as "+ wordlists1[index2]+"    is to___?"
print myquestion

答案 4 :(得分:1)

使用随机选择两个单词索引并使用这些索引生成问题并检查答案是否正确如下:

import random
def makeQuestion():
    indexes = range(len(wordlists1))
    word1 = random.choice(indexes)
    word2 = random.choice(indexes)
    ans = raw_input("{} is to {} as {} is to___? ".format(wordlists1[word1], wordlists2[word1], wordlists1[word2]))
    if ans.strip().lower() == wordlists2[word2]:
       print True
    else:
       print False

演示:

>>> wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
...              "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]
>>> wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
...          "pay attention", "sell", "fail", "accept", "allow", "include"]
>>> import random
>>> def makeQuestion():
...     indexes = range(len(wordlists1))
...     word1 = random.choice(indexes)
...     word2 = random.choice(indexes)
...     ans = raw_input("{} is to {} as {} is to___? ".format(wordlists1[word1], wordlists2[word1], wordlists1[word2]))
...     if ans.strip().lower() == wordlists2[word2]:
...        print True
...     else:
...        print False
... 
>>> makeQuestion()
succeed is to fail as sad is to___? happy
True
>>> makeQuestion()
prevent is to allow as ignore is to___? pay attention
True
>>> makeQuestion()
exclude is to include as heavy is to___? cold
False

答案 5 :(得分:1)

from random import randint

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
             "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
         "pay attention", "sell", "fail", "accept", "allow", "include"]

index1 = randint(0, len(wordlists1) - 1)

index2 = randint(0, len(wordlists2) - 1)

answer = wordlists2[index2]

print ("Q : %s is %s as %s is to ________ ? " % (wordlists1[index1], wordlists2[index1], wordlists1[index2]))

user_input = raw_input("A : ")

if user_input.lower() != answer:

        print ("Answer is %s" % answer)
else:

        print ("Correct Answer")

答案 6 :(得分:0)

生成从0到列表长度的随机数。此数字表示从列表中选择随机索引。一旦你有了#34;随机"选择你的话,只需在你的问题中使用它们

答案 7 :(得分:0)

from __future__ import print_function
import random

__author__ = 'lve'

wordlists1 = ["hot", "summer", "hard", "dry", "heavy", "light", "weak", "male",
              "sad", "win", "small", "ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
              "pay attention", "sell", "fail", "accept", "allow", "include"]

answer_string = ''

random_question_index = random.randrange(len(wordlists1))

answer_string += '{} is to {} as '.format(wordlists1.pop(random_question_index).capitalize(), wordlists2.pop(random_question_index))

random_answer_index = random.randrange(len(wordlists1))
answer_string += '{} is to___? \nAnswer is {}'.format(wordlists1.pop(random_question_index),
                                                      wordlists2.pop(random_question_index).upper())
print(answer_string)