繁体   English   中英

PYTHON:random.py中的错误消息? 曾经工作过,然后没有了,然后又做了,现在又没有了。 发生了什么?

[英]PYTHON: Error Message in random.py? Used to work, then it didn't, then it did, now it doesn't again. What's happening?

我已经学习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')

这就是让您:

term_list = [...]

是在文件的开头定义的,但是稍后在输入的数量为最大值时执行以下操作。

test_term = term_list 

这不会创建数组的副本,而是会创建两个引用同一数组的变量。 因此,对test_term任何进一步修改实际上都会反映在两个变量所引用的列表中。

而且由于您是在脚本的全局级别定义test_terms ,因此您在进行此调用时会对其进行NUKE

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()一个对象。

同样,全球人士通常被认为是一件坏事,值得商de。 C语言中的全局变量一样,有时它们有明确的目的,但是在大多数情况下,它们会隐藏错误并使问题难以跟踪。 同样,有时您的变量声明会遮盖全局变量,而其他变量声明(如您所见)会让您搞砸。

好吧,很明显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_listlist_scrambler的开头仍然是完整的,但是在list_scrambler结束时为空。

        del unscrambled_list[transfer_var]

一次删除term_list所有元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM