如何生成不重复或重复的随机整数?

时间:2015-10-21 18:01:16

标签: function random integer duplicates

我需要找到以下程序的解决方案。赋值要求我修改decisionrs()函数以返回不重复的随机整数。这是为了不允许在程序中重复任何问题。

下面是代码:

#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!")

有什么想法吗?

0 个答案:

没有答案