返回循环中带有if / else语句的while循环的开头

时间:2017-12-08 03:02:18

标签: python python-3.x

我还在上学,这是一个老师想要的实验室问题。 我想用随机整数填充数组,但没有重复。数组需要是一定长度,这意味着如果生成的整数已经在列表中,则需要返回random.randint(x,y)语句并再次执行。 (这只是代码片段.ax是数组,nx = 21,x = 10,y = 40)

import random
def fill(ax,nx, x,y): # fill the list a with n random integers in the range x --- y inclusive BUT NO duplicates permitted THIS code NEEDS to be fixed
    j = 0
    while (j < nx):  # populate the array using the while loop
        x = random.randint(x, y)  # Return a random integer .
        if x not in ax:
            ax.append(x)
            j = j + 1
        else:
            ??????

    return ax

这是我被困的地方。如果生成的整数已经在列表中,则需要有一个else语句返回到while循环的开头。我想不明白。我试过继续,但没有做任何事情。评论来自老师。我觉得他建议使用goto语句,但这非常令人不悦,甚至在python中都没有。这是我发布的第一个问题所以请温柔!

0 个答案:

没有答案
相关问题