【发布时间】:2016-01-22 14:28:01
【问题描述】:
我有一个程序,本质上是针对学生的考试程序。该程序包含两个包含相反含义的单词的列表,它们都在相同的位置索引中。例如,list1 中的 hot 和 list2 中的冷都在索引 0 中。
我创建了一个函数,允许随机生成并打印问题以供用户完成。检查答案以确保用户给出的组合是正确的。我现在正在尝试创建一个函数,以确保程序中的问题不会重复,我正在努力。
我尝试创建一个函数,将随机选择的单词对存储在一个变量中,然后将其附加到一个列表中。在每个问题之后,函数应该检查列表中的变量,如果变量存在,则变量调用重新生成新对的函数。这种情况一直持续到所有问题都完成并且没有重复。程序正确地将变量存储在列表中(我通过打印列表进行了检查),但是当存在重复时,配对生成器功能不起作用,并且问题中出现重复。
下面是我要创建的函数的代码,以及随机对生成器和列表的代码。任何想法,我需要尽快帮助。
功能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()
功能 B:
def deciders():
global countdown
countdown = len(opposite1) - 1
global decider
decider = random.randint(0,countdown)
global decider2
decider2 = random.randint(0,countdown)
注意:决策者功能在程序中的其他功能之前,列表如下:
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"]
有什么想法吗?
【问题讨论】:
-
请提供更清晰的信息。阅读this
标签: python list function random duplicates