当我使用randrang时,我的进化人工智能会出现随机错误

2024-04-19 07:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行这个代码,它是一个“人工智能”,知道答案是10。每次我运行它,它有时会给我这个错误。这是在python3.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

Tags: theanswerinmiddleforisthisanswers
1条回答
网友
1楼 · 发布于 2024-04-19 07:19:52

首先观察randrange将引发ValueError,如果它的第二个参数不大于第一个参数:

randrange(0,0,1) # always raises ValueError

接下来,注意你有一行代码,它与

randrange(-x,x,1)

然后观察如果x==0最后一行有效地变成randrange(0,0,1)。我的x与你的randevalrange类似

总之,确保randevalrange永远不能是0

相关问题 更多 >