gpt4 book ai didi

python-2.7 - 避免在 python 中重复回答

转载 作者:行者123 更新时间:2023-12-03 16:36:55 26 4
gpt4 key购买 nike

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

What is: Exception 1- Another name for runtime error 2- Another name for runtime error 3- The meaning of a program

那么如何避免这种重复呢?

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

最佳答案

现在您没有做任何事情来确保第二个和第三个选择的选项不会与第一个发生冲突。为此,您可以在选择第二个之前从列表中删除第一个选择的选项,依此类推。

然而,python 标准库中有函数已经实现了从列表中仔细选择多个项目:

answers = random.sample(F2c, 3)

或者选择索引:

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

关于python-2.7 - 避免在 python 中重复回答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22981033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com