2015-01-08 54 views
0

我有兩個列表:的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) 
+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) 
0

產生從0隨機數到你的列表的長度。這個數字代表從您的列表中選擇一個隨機索引。一旦你已經 「隨機」 挑選你的話,只需要使用他們在你的問題

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 
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.choice切換到random.sample(t, 2)。這是更好的方法。 (由DSM推薦,但我想更新我的代碼)。

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

這顯然是最好的答案。以這種方式使用樣本避免了兩次對SAME元素進行採樣的情況,這在樣本/選擇被調用兩次的答案中是可能的。 – lightalchemist

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