jeudi 22 octobre 2015

How do I check for a string element stored in a variable in a list?

I have a program that is essentially a examination program for students. The program contains two lists that contain words of the opposite meanings, all in the same positional index. For example hot in list1 and cold in list2 both in index 0. I have created a function which allows the question to be randomly generated and printed for the user to complete. The answers are checked to ensure that combination the user has given is correct. I am now trying to create a function that ensures that no questions in the program are repeated, and i am struggling. I attempted to create a function that stores the randomly chosen word pairs in a variable, which is then appended into a list. After each question the function is supposed to check for the variable in the list, and if the variable is present, the variable calls for the function which re-generates a new pair. This continues until the questions are all done and there are no duplicates. The program correctly stores the variables in the list (I checked by printing the list) but when there is a duplicate, the pair generator function doesn't work, and duplicates in the questions are made. The code to the function I am trying to create is below, and also for the random pair generator and the list. Any Ideas, I need help on this as soon as possible.

Function A:

def randcheck():
    global dcheck4
    dcheck = opposite1[decider]
    dcheck2 = opposite2[decider]
    dcheck3 = opposite1[decider2]
    dcheck4 = opposite2[decider2]
    dchecklist.append(dcheck)
    dchecklist.append(dcheck2)
    dchecklist.append(dcheck3)
    dchecklist.append(dcheck4)
    print(dchecklist)

    while dcheck in dchecklist:
        deciders()    
    while dcheck2 in dchecklist:
        deciders()
    while dcheck3 in dchecklist:
        deciders()
    while dcheck4 in dchecklist:
        deciders()

Function B:

def deciders():
    global countdown
    countdown = len(opposite1) - 1
    global decider
    decider = random.randint(0,countdown)
    global decider2
    decider2 = random.randint(0,countdown)

Note: The deciders function comes before the other function in the prorgam, and the list is bellow:

global opposite1
opposite1 = ["hot", "summer", "hard", "dry", "heavy", "bright", "weak", "male", "sad", "win", "small", "ignore", "buy", "succeed", "reject", "prevent", "exclude"]
global opposite2
opposite2 = ["cold", "winter", "soft", "wet", "light", "dark", "strong", "female", "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"]

Any ideas?




Aucun commentaire:

Enregistrer un commentaire