Skip to main content
 首页 » 编程设计

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

2025年01月19日5lori

我正在运行这段代码,它是一个“人工智能”,它知道答案是 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 

请您参考如下方法:

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

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

接下来观察你有一行代码同构于

randrange(-x,x,1) 

然后观察如果 x==0 最后一行实际上变成了 randrange(0,0,1)。我的 x 是你的 randevalrange 的模拟。

总而言之,确保 randevalrange 永远不会是 0