Hi, so I just have a question that I at least think will be pretty easy. So, I am making a script that basically gives you a "vocabulary test". There is just one feature I would like to implement that I need help with.

I would like to make it randomized. I am using a dictionary for all my words/definitions, and I know that the dictionary does not keep them in order, however it is not randomized.

What would be the easiest way to randomize the order in which the questions are asked?

Snippet of code: ("words" is a dictionary)

def vocabtest():
    for a, r in words.items():
        Blah
        Blah

Thank you in advance.

Here is an easy way!

import random
d = {}#put your dic here

for f in random.shuffle(d.keys()):
    print d[f], f

That shuffles the keys up and then you just access the values from the dictionary with the keys.

Thanks paulthom, however I get a TypeError when I add in those lines.

Full error:
for f in random.shuffle(words.keys()):
TypeError: 'NoneType' object is not iterable

Any idea what is wrong?

Thanks.

Woops i stuffed up a bit, scuse me.
Here is the new code:

import random

d = {'1':1,'2':2}
keys= d.keys()
random.shuffle(keys)
for f in keys:
    print d[f], f

Well, when I use that code, it simply does nothing. I have no idea what is wrong...I have tried shuffling both the keys and the values, as well as items.

Simplified Code:

def vocabtest():
    random.shuffle(words.items())
    for a, r in words.items():
        print "What word does the following definition correspond with?"
        print a
        answer = raw_input("> ")
        if words[answer] == r:
            print "Correct!"
        else:
            print "Fail"

I tested two versions of the code in two different shells: one with the "random.shuffle..." line, and one without. And, they both output the words in the same exact order. Am I doing some idiot mistake or what is wrong?

Your problem is that you go random.shuffle(words.items())
You cant change words.items. It is a function. So what you have to do instead is make a list of the keys and then shuffle those

def vocabtest():
    word  = words.items()
    random.shuffle(word)
    for w in word:
        a = words[w]
        print "What word does the following definition correspond with?"
        print a
        answer = raw_input("> ")
        if words[answer] == r:
            print "Correct!"
        else:
            print "Fail"

Sorry for all the trouble paulthom, but I am still getting errors. Now I am getting Key Error at random words in my dictionary.

Error:
print words[w]
KeyError: ('inert', 'without power to move or act; lifeless; exhibiting no chemical activity; unreactive')

It does not only happen with the word inert, but with a variety of words. Well hey at least now I know the dictionary is randomized!

However, may you please help me again?

Thanks.

woops i see my mistake. Just replace the line

word = words.items()
##WITH
word = word.keys()

My mistake there.

Well...big step! No errors! However, the script does not seem to like matching variables.

What word does the following definition correspond with?
cheap and gaudy in nature or appearance; showy; sleazy
> tawdry
That is incorrect. The answer was tawdry

Code:

def vocabtest():
    word = words.keys()
    random.shuffle(word)
    for w in word:
        a = words[w]
        print "What word does the following definition correspond with?"
        print a
        answer = raw_input("> ")
        answer = answer.strip()
        if words[answer] == w:
            print "Correct!"
        else:
            print "Fail."

This should work.. i think the r was in the wrong place

def vocabtest():
    word  = words.keys()
    random.shuffle(word)
    for w in word:
        a = words[w]
        print "What word does the following definition correspond with?"
        print a
        answer = raw_input("> ")
        if w.lower() == answer.lower():
            print "Correct!"
        else:
            print "Fail"

Here's to hoping that works!

Dang nice try. But still nothing. I even tried stripping the answer. Nada!

Try printing out what the answer is meant to be and what you are getting through the raw_input() that will show what is wrong quite easily.

commented: Continued, fantastic support. S/he followed through well, also. +1

Well, it is finally working. The problem was not with the function itself, but I have the script run a different function if the word is not correct. Somehow, that other function must have been editing the answer input by the user.

Well, thank you for all the continued help paulthom! Rep well deserved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.