我已经学了几个月的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
现在,通常情况下,它会像你期望的那样输出所有东西。我还没有自动生成问题的功能,所以我必须输入以下行:
^{2}$或者不管我用什么术语。在
以下是我得到的输出:
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')
很明显,}是空的,对吧?那么
randint()
在抱怨,因为{是
^{pr2}$并且
randint
被卡住了。那么为什么是空的?因为这句话:破坏
term_list
,这可能是;-)无意的。在很难按照代码来追踪为什么会发生这种情况。基本问题是
gen_test
可以设置然后以名称}。那么
test_terms
返回{term_list
在list_scrambler
的开头仍然是完整的,但是在list_scrambler
结束时是空的。在一次删除
term_list
中的所有元素。在这就是你想要的:
在文件开始时定义,但稍后当输入的金额为最大值时,请执行以下操作
^{pr2}$这不会创建数组的副本,而是创建两个变量,它们都引用同一数组。因此,对
test_term
的任何进一步修改实际上都反映在这两个变量引用的列表中。在由于您在脚本中定义了一个全局级别的
test_terms
,所以当您进行此调用时,您将对其进行核武处理还要补充一下
匈牙利符号是个大禁忌,而python无论如何都是一种强类型语言。如果你很难跟踪时间,不要依赖变量名。取而代之的是给你自己一个IDE或者用一个能表达他们正在做什么的名字。在
应该重写为
这是一个更多的偏好,但当打印出需要数据浮动的文本时,您可以省去一些头痛和写作
这将把变量填充到{0}中,并为您提供一些格式化上下文,防止您不得不
str()
对象。在而且,全局变量通常被认为是一件不好的事情,值得商榷。就像C中的global一样,有时它们的作用是明确的,但在大多数情况下,它们隐藏了bug,使问题更难跟踪。有时你的变量声明会隐藏全局变量,其他的(如你所见)会让你把事情搞砸。在
相关问题 更多 >
编程相关推荐