当我使用randrange时,我的进化ai会出现随机错误

时间:2017-10-09 06:28:58

标签: python machine-learning

我正在运行此代码,这意味着要成为一个' ai'知道答案是10.每次我运行它,它有时会给我这个错误。这是在Python 3.6.3

错误:

, 1)
  File "D:\Jonte\python\lib\random.py", line 198, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0,0, 0)

代码:

from random import randrange
# middle = randrange(0,2)  <- this is potential code
# the reason i have the question minus the answer is so that they are related in some way
# output = question - middle <- this is potential code for the middle  analysis
answer = 10
'''
reminder create a list for the first using randrange then use the method 
described
'''
correctanswers = []
# evaluation of the output to the correct answer
def cauculate_answers(i, question): #x is a list holder
    while i != 11:
        if question[i] >= 6:
            correctanswers.append(question[i])
        if question[i] == 10:
            print("right")
        else:
            print("wrong")
        i += 1
questionlist = [randrange(1, 11), randrange(1, 11), randrange(1, 11), 
randrange(1, 11), randrange(1, 11), randrange(1, 11), randrange(1, 11), 
randrange(1, 11), randrange(1, 11), randrange(1, 11), randrange(1, 11)]        
cauculate_answers(0, questionlist)
print ()
print()
print(correctanswers)
def incrementanswers(correctans):
    i = 0
    length = len(correctans)
    while i != length :
        randevalrange = answer - correctans.pop(i)
        apeendvalue = correctans[i] + randrange( randevalrange * -1, randevalrange, 1)
        correctans.append(apeendvalue)
        i = 1 + i

# run through this as long as correct answers
# each number gets minused off answer to find incremental value
# then use that with this algorithm answer-correctanswers[i]= randevalrange
# then store all of this data in a temporary dictionary
# then delete all data in correct answers then fill questionlist with
# randrange(randevalrange, -randevalrange) this repeated twice for each
# incremental value
#
x = 0
while x != 10:
    incrementanswers(correctanswers)
    print (cauculate_answers(0, correctanswers))

#if i return the succesfull questions i have to feed it a dictionary
#it wont work until you figure out the next bit

1 个答案:

答案 0 :(得分:1)

首先观察ValueError如果第二个参数不大于第一个参数,将引发randrange(0,0,1) # always raises ValueError

randrange(-x,x,1)

接下来观察你有一行与

同构的代码
x==0

然后观察如果randrange(0,0,1)最后一行有效变为x。我的randevalrangerandevalrange类似。

总之,请确保0永远不会是{{1}}。