In desperate need of Python help...
For my Python class, we have to write a function that makes a vocabulary quiz. It has to give the user a word, and 4 answer choices (3 random and 1 correct in a random order). Then it has to ask for input, and add a point to a tally if the user chooses the right answer. I am about as lost as I can possibly be on this. I feel like I was given a pointy stick and thrown into a fight with a grizzly bear.
This is what I have so far:
import random
vocab = [("a","a1"),
("b","b1"),
("c","c1"),
("d","d1"),
("e","e1"),
("f","f1")]
def choices():
- choice = []
- for i in range(3):
-- choice.append(vocab[random.randint(0,5)][1])
- return choices
def quiz():
- points = 0
- for (chunk) in vocab:
-- random.shuffle(chunk[0])
-- print(chunk[0])
- guess = input(str("What is the correct definition?: "))
- if guess == answer:
-- points += 1
-- print("Correct!")
- else:
-- print("Wrong!")
My "choices" function doesn't work. It just returns "<function choices at 0x17d0a50>", and my quiz function prints a through f. I can't figure out how to only make it print one at a time. Hopefully someone here can point me in the right direction, because my teacher doesn't check her e-mail on weekends...
def choices():
- choice = []
- for i in range(3):
-- choice.append(vocab[random.randint(0,5)][1])
- return choices
My "choices" function doesn't work. It just returns "<function choices at 0x17d0a50>"
What behaviour are expecting?
You have a function called 'choices' plural
You create an array called 'choice' singular
But at the end of the function you return the function 'choices' plural, do you mean to return the array you have just created (choice singular) instead of the function?
Oops, you're right. I fixed it now so that it works correctly, but now I have to make it so that it doesn't display the same choice more than once... and I don't know how...
def choices():
- choices = []
- for i in range(3):
-- choices.append(vocab[random.randint(0,5)][1])
- return choices
def quiz():
- points = 0
- for (chunk) in vocab:
-- random.shuffle(chunk[0])
-- print(chunk[0])
and my quiz function prints a through f. I can't figure out how to only make it print one at a time.
I'm not familiar with python, but it looks like your print command is inside of your for loop, so the print command is being executed multiple times.
Are you programming in a proper IDE like eclipse? If you are you should be able to set breakpoints and watches then step through the program to see what is going wrong.
If you aren't sure how to do that try googling
eclipse python debug set watch
or set breakpoint
just replace eclipse with the name of whatever IDE you use, there will be many tutorials.

- choice = []
- for i in range(3):
-- choice.append(vocab[random.randint(0,5)][1])
- return choice
I'm not quite sure what you're trying to do here. The function choices() as written here looks like it will return a list of 3 randomly chosen second parts of the elements in vocab.
If you're trying to get a list of 3 items without repeats, you need some way to prevent it from returning a duplicate. Keeping a list of already used elements and using some code to reselect if you picked something that was previously used would work. Probably a better way would be to have a list of possible choices and delete previously used choices. If you want to try deleting elements from a list, you probably will want to make a temporary list to hold your options, since you probably don't want to change your global vocab list.
Your variable naming could be improved here. It doesn't matter to the computer what you name your variables, it would be perfectly happy with xyz12354 for a variable name, but it does matter to people, and bad variable names can be very confusing.
The function choices() and the list choice[] have names that are too similar. It's too easy to confuse them. I'd probably name the list choices[], since it contains several choices, and rename the function. Maybe to choose(), since verbs make good function names (in general), but that's still pretty similar. You understand what you're trying to do better than I do, so you can probably come up with a better name than I did.
- points = 0
- for (chunk) in vocab:
-- random.shuffle(chunk[0])
-- print(chunk[0])
Again with the variable names: chunk refers to a single element in vocab. When I think of a vocabulary and imagine selecting a particular vocabulary element, I think of the word 'word', so I'd suggest renaming chunk to word.
What your call to random.shuffle() is trying to shuffle is a single element of your vocabulary, so it's probably not actually doing anything. How many different ways can you arrange a single element in order? Only one, because you can't rearrange the order of one object. This shows how powerful good naming can be in programming; since if you had named chunk word (or vocab_element or something), then you probably would have caught that yourself.
Lastly, if I understand what you're trying to do here, you probably want to do the random.shuffle() call outside of the loop, since if you changed it from how it is now to reshuffling the entire vocab list, it would end up getting reshuffled every time you went through the loop.
_________________
"A dead thing can go with the stream, but only a living thing can go against it." --G. K. Chesterton
Are you programming in a proper IDE like eclipse? If you are you should be able to set breakpoints and watches then step through the program to see what is going wrong.
I think I know what you're talking about... Python has a thing called "PyScripter" but unfortunately I have a Mac, and it's Windows-only.
Yes. That's what I'm trying to do.
What your call to random.shuffle() is trying to shuffle is a single element of your vocabulary, so it's probably not actually doing anything. How many different ways can you arrange a single element in order? Only one, because you can't rearrange the order of one object. This shows how powerful good naming can be in programming; since if you had named chunk word (or vocab_element or something), then you probably would have caught that yourself.
Lastly, if I understand what you're trying to do here, you probably want to do the random.shuffle() call outside of the loop, since if you changed it from how it is now to reshuffling the entire vocab list, it would end up getting reshuffled every time you went through the loop.
Thank you! This should help me quite a bit.
I actually sat down last night and wrote down exactly how I'm planning my program to run.
x = [0,1,2,3,4,5] <-- this is a counting list that will be used for the words
y = [0,1,2,3,4,5] <-- this is a counting list that will be used for the definitions
choices = []
points = 0
Pick a random number n from list x
display the corresponding word from the vocab list
store the corresponding definition in a variable called "answer"
place "answer" in "choices"
place 3 other random, unique definitions in "choices"
(which i plan to do by first deleting n from list y, and each time i pick a number m from list y, i delete it from list y)
shuffle "choices"
display "choices"
ask the user for their "guess"
gauge if "guess" == "answer"
Add 1 to "points" if they are equal
delete n from list x
erase everything in choices
add all values back into y
repeat until list x is blank, in which case display the number of points
Hopefully this sounds doable...
Are you programming in a proper IDE like eclipse? If you are you should be able to set breakpoints and watches then step through the program to see what is going wrong.
I think I know what you're talking about... Python has a thing called "PyScripter" but unfortunately I have a Mac, and it's Windows-only.
There are a gazillion editors and IDEs that will run on a mac
http://wiki.python.org/moin/PythonEditors
If you use Eclipse & pyDev there are even a some videos to teach you how to use the debugging tools