0

I have two lists:

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

I use random.choice to pick a word from each list. Once I have the words, I need to print them as a question. For e.g., if hot and weak are selected it should print: "Hot is to cold as weak is to___?"

I really need help on this, and detailed steps would be appreciated.

My code:

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)
lightalchemist
  • 10,031
  • 4
  • 47
  • 55
iHazo
  • 29
  • 3

8 Answers8

4

I might do something like

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

First, I'd combine the two lists into a list of pairs:

>>> 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')]

And then I'd use random.sample to choose two of them:

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

One major advantage of using random.sample here is that you don't have to worry about drawing the same pair twice (no "weak is to strong as weak is to ?" questions.)

After this, we can make a question string:

>>> "{} is to {} as {} is to ?".format(example[0], example[1], question[0])
'weak is to strong as heavy is to ?'
DSM
  • 342,061
  • 65
  • 592
  • 494
  • 1
    This is clearly the best answer. Using sample in this manner avoids the case of sampling the SAME element twice, which is possible in the answers where sample/choice is called twice. – lightalchemist Jan 08 '15 at 05:29
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 print something like dry is to wet as ignore is to ___? (pay attention)

Update: I switched to random.sample(t, 2) from random.choice. It's the better way to do this. (As Suggested by DSM, but I wanted to update my code as well).

Prashant
  • 1,014
  • 11
  • 28
1

You used random.choice twice, which makes randomword1 different from randomword2 in terms of position in your lists. Use random.randint instead to get a unified index each time:

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)
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
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
Anurag Goel
  • 468
  • 10
  • 15
1

Using random choose two word indexes and with those indexes generate question and check if the answer is correct or not like this:

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

Demo:

>>> 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
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
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")
吕祥钊
  • 156
  • 5
hariK
  • 2,722
  • 13
  • 18
0

generate a random number from 0 to length of your lists. This number would represent choosing a random index from your lists. Once you have "randomly" chosen your words, just use them in your questions

lollerskates
  • 964
  • 1
  • 11
  • 28
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)
吕祥钊
  • 156
  • 5