mercredi 21 octobre 2015

How do I generate random integers that are not repeated or duplicated?

I need to find a solution to the following program. The assignment asks for me to modify the deciders() function to return random integers that are not repeated. This is to allow no questions to be repeated in the program.

Heres the code:

#Importing Variables and Importing Functions
import random
score = 0
question_no = 1

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


#Defining Functions:
def deciders():
    global decider
    decider = random.randint(0,16)
    global decider2
    decider2 = random.randint(0,16)

def question():
    global answer
    answer = input("Q{}. '{}' is to '{}' as '{}' is to     :".format(question_no,opposite1[decider],opposite2[decider],opposite1[decider2]))


#Main Body of Program:
name = input("Enter your full name:")
class_name = input("Enter the name of your class:")
class_name = class_name.lower()
class_name = class_name.strip()
yes_no = input("In this examination, you will be given a pair of words that     are antonyms (opposites) of each other. You will then be given another word,    which you are to write the antonym of. Do you wish to begin? (Yes/No):")
yes_no = yes_no.lower()
yes_no = yes_no.strip()
if yes_no == 'yes':
    while question_no <= 10:
        global answer
        deciders()
        question()
        if answer == opposite2[decider2]:
            print("Correct!")
            score += 1
        else:
            print("Incorrect!")
        question_no += 1
        if question_no > 10:
            print("The examination is complete. \n {} scored     {}/10".format(name,score))
    if class_name == "bear":
        bear = open("bear.txt","a")
        bear.write("{}, {}/10 \n".format(name,score))
        bear.close()
    elif class_name == "duck":
        duck = open("duck.txt","a")
        duck.write("{}, {}/10 \n".format(name,score))
        duck.close()
    elif class_name == "bee":
        bee = open("bee.txt","a")
        bee.write("{}, {}/10 \n".format(name,score))
        bee.close()
    else:
        print(" The class that you had entered was not valid, therefore your     score will not be saved.")
else:
    print("Goodbye!")

Any ideas?




Aucun commentaire:

Enregistrer un commentaire