避免在python中重复回答

时间:2014-04-10 07:10:16

标签: python-2.7

我正在制作闪存卡程序,以帮助我记住python关键字和术语。但有时它会为我重复这个答案,这对任何人来说都是显而易见的。

  

什么是:例外       1-运行时错误的另一个名称       2-运行时错误的另一个名称       3-程序的含义

那么如何避免这种重复?

while count < 10:
os.system('clear')
wordnum = random.randint(0, len(F1c)-1)
print "What is:  ", F1c[wordnum], ""
options = [random.randint(0,len(F2c)-1),random.randint(0,len(F2c)-1),
random.randint(0,len(F2c)-1)]
options[random.randint(0, 2)] = wordnum
print '1 -', F2c[options[0]],
print '2 -', F2c[options[1]],
print '3 -', F2c[options[2]],
answer = input('\nYou  choose number ?:')
if options[answer-1] == wordnum:
    raw_input('\nCorrect! Hit enter...')
    score = score + 1
else:
    raw_input('\nWrong! Hit enter...')
count = count + 1
print '\nYour score is:', score

1 个答案:

答案 0 :(得分:3)

现在你什么都不做,以确保第二个和第三个选定的选项不会先与第一个冲突。为此,您可以在选择第二个之前从列表中删除第一个选择的选项,依此类推。

但是,python标准库中的函数已经实现了从列表中仔细选择几个项目:

answers = random.sample(F2c, 3)

或者选择索引:

options = random.sample(range(len(F2c)), 3)