我现在已经学习了几个月的python,通常我已经能够克服我面临的所有问题,但现在我不知所措。我正在编写一个名为“Quizzer”的节目。这将用于根据Python给出的术语和答案列表生成随机问题。 我的主要问题在于我一直在研究的gen_question函数。我希望Python接收一个术语,并输出四个多项选择答案:一个是实际的,三个是从所有可能答案的池中随机选择的。我必须包括几个检查,以确保所选的随机答案不是真正的答案,并且彼此不一样。
我今天终于开始工作了,稍后我测试了它。我收到一条错误消息(我将在一秒钟内显示)。我把一切都解开了,回到了我之前的地方,我仍然得到了同样的错误信息。几个小时后我回来了,我又得到了它。出于沮丧,我重新尝试了,它起作用了。现在它已经不再适用了。请任何人:发生了什么事?
这是我的代码(我不知道什么是必要的,所以我包括整个事情):
#import random for generating
import random
#term and definition libraries
terms_ans ={'term1':'answer1','term2':'answer2','term3':'answer3','term4':'answer4','term5':'answer5','term6':'answer6','term7':'answer7','term8':'answer8','term9':'answer9','term10':'answer10','term11':'answer11','term12':'answer12','term13':'answer13','term14':'answer14','term15':'answer15','term16':'answer16','term17':'answer17','term18':'answer18','term19':'answer19','term20':'answer20'}
term_list = ['term1','term2','term3','term4','term5','term6','term7','term8','term9','term10','term11','term12','term13','term14','term15','term16','term17','term18','term19','term20']
answer_list = ['answer1','answer2','answer3','answer4','answer5','answer6','answer7','answer8','answer9','answer10','answer11','answer12','answer13','answer14','answer15','answer16','answer17','answer18','answer19','answer20']
#picks the test questions to ask
def gen_test(amount=len(term_list)):
found_starter = False
test_terms = []
while found_starter == False:
#pick a random starting point in the terms to see if it is suitable
start_point = random.randint(1, len(term_list))
if amount == len(term_list):
#if user inputs max amount of questions possible, just take the term list
test_terms = term_list
found_starter = True
elif len(term_list) - (start_point + amount) >= 0:
#if it is suitable, then append the terms to the test questions
for x in xrange(start_point,start_point+amount):
test_terms.append(term_list[x])
found_starter = True
else:
return test_terms
#scramble list
def list_scrambler(unscrambled_list):
test_terms=[]
countdown = len(unscrambled_list) + 1
for x in range(1, countdown):
transfer_var = random.randint(0,len(unscrambled_list)-1)
test_terms.append(unscrambled_list[transfer_var])
del unscrambled_list[transfer_var]
return test_terms
#ask user for amount of questions needed and get the list
test_terms = list_scrambler(gen_test(int(raw_input("How many questions on your test? (There are " + str(len(term_list)) + " questions in total.) "))))
def gen_question(picked_term, question_num=1, total_amount=len(test_terms)):
#print start of question
print
print "Question " + str(question_num) + " of " + str(total_amount) + ":"
print
print picked_term
print
#gather random multiple choice answers they must a) all be different and b) not be the answer
ans_1_acceptable = False
while ans_1_acceptable == False:
int_rand_ans_1 = random.randint(1, len(term_list)) - 1
if str(term_list[int_rand_ans_1]) != str(picked_term):
#Term accepted; send to output
ans_1_acceptable = True
ans_2_acceptable = False
while ans_2_acceptable == False:
int_rand_ans_2 = random.randint(1, len(term_list)) - 1
if int_rand_ans_2 != int_rand_ans_1 and str(term_list[int_rand_ans_2]) != str(picked_term):
ans_2_acceptable = True
ans_3_acceptable = False
while ans_3_acceptable == False:
int_rand_ans_3 = random.randint(1, len(term_list)) - 1
if int_rand_ans_3 != int_rand_ans_1 and int_rand_ans_3 != int_rand_ans_2 and str(term_list[int_rand_ans_3]) != str(picked_term):
ans_3_acceptable = True
#Decide if the correct answer is A, B, C, or D
correct_ans = random.randint(1,4)
#Print the options using the variables gathered above
if correct_ans != 1:
print "A) " + answer_list[int_rand_ans_1]
else:
print "A) " + terms_ans[picked_term]
if correct_ans != 2:
print "B) " + answer_list[int_rand_ans_2]
else:
print "B) " + terms_ans[picked_term]
if correct_ans != 3:
print "C) " + answer_list[int_rand_ans_3]
else:
print "C) " + terms_ans[picked_term]
if correct_ans == 1:
print "D) " + answer_list[int_rand_ans_1]
elif correct_ans == 2:
print "D) " + answer_list[int_rand_ans_2]
elif correct_ans == 3:
print "D) " + answer_list[int_rand_ans_3]
else:
print "D) " + terms_ans[picked_term]
print
现在,通常会输出像您一样的所有内容。我没有自动生成问题的功能,所以我必须输入以下内容:
gen_question('term1')
或我使用的任何术语。
以下是我得到的输出:
How many questions on your test? (There are 20 questions in total.) 20
>>> gen_question('term1')
Question 1 of 20:
term1
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
gen_question('term1')
File "C:\Users\Owner\Desktop\LEARNING PYTHON\scripts\in progress\Quizzer.py", line 69, in gen_question
int_rand_ans_1 = random.randint(1, len(term_list)) - 1
File "C:\Users\Owner\Desktop\LEARNING PYTHON\python 2.7.5\lib\random.py", line 241, in randint
return self.randrange(a, b+1)
File "C:\Users\Owner\Desktop\LEARNING PYTHON\python 2.7.5\lib\random.py", line 217, in randrange
raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
ValueError: empty range for randrange() (1,1, 0)
>>> gen_question('term8')
答案 0 :(得分:1)
这就是让你:
term_list = [...]
在文件的开头定义,但稍后在输入的数量为最大值时执行以下操作。
test_term = term_list
这不会创建数组的副本,这会创建两个引用相同数组的变量。因此,对test_term
的任何进一步修改实际上都反映在两个变量引用的列表中。
由于您在脚本中的全局级别定义test_terms
,因此您在进行此调用时会对其进行NOUKE
def list_scrambler(unscrambled_list):
test_terms=[]
countdown = len(unscrambled_list) + 1
for x in range(1, countdown):
transfer_var = random.randint(0,len(unscrambled_list)-1)
test_terms.append(unscrambled_list[transfer_var])
del unscrambled_list[transfer_var]
return test_terms
另外,
匈牙利语符号是一个很大的禁忌,而python则是一种强类型语言。如果您很难跟踪时间,请不要依赖变量名称。而是让自己成为一个IDE或使用表达他们正在做的事情的名称。
if something == false:
应改写为
if not something
这个更适合偏好,但是当打印出需要放入数据的文本时,你可以省去一些头痛并写下来
"D) {0}".format(somelist[index])
这会将变量填入{0}并为您提供一些格式化上下文,并阻止您拥有str()
个对象。
另外,一般来说全局变量被认为是 坏 的东西,它们是值得商榷的。像 C 中的全局变量有时它们有明确的目的,但是大多数情况下它们会隐藏错误并使问题更难跟踪。有时你的变量声明会影响全局变量,其他的(如你所见)会让你搞砸了。
答案 1 :(得分:0)
嗯,很明显,randint()
抱怨是因为term_list
是空的,对吗?然后
random.randint(1, len(term_list))
是
random.randint(1, 0)
并且randint
被卡住了。那么为什么 term_list
为空?这是因为这句话:
test_terms = list_scrambler(gen_test(int(raw_input("How many questions on your test? (There are " + str(len(term_list)) + " questions in total.) "))))
摧毁term_list
,这可能;-)无意。
很难按照代码来追踪发生这种情况的原因。基本问题是gen_test
可以设置
test_terms = term_list
然后以term_list
名称返回test_terms
。然后term_list
在list_scrambler
开始时仍然完好无损,但在list_scrambler
结束时为空。在
del unscrambled_list[transfer_var]
一次删除term_list
中的所有元素。