无法创建一个随机列表来计算其元素之间的最长距离

Can't create a random list that calculates the longest distance between its elements

I can't figure out why I'm getting this error: Traceback (most recent call last): line 78, in find_max_gap(x) line 22, in find_max_gap vmin = x[0] TypeError: 'int' object is not subscriptable

I'm new to Python so It's difficult for me to track the mistakes I make. I've been trying to figure out what's going on the whole day

User inputs the parameters and creates a random list that calculates the longest distance among its elements.

import random

def generate_random_floats(n,m,seed):
    x = []
    if (y==0):
        random.seed()
        for i in range (n):
            x.append(random.uniform(-m,m))
    else:
        random.seed(y)
        for i in range (n):
            x.append(random.uniform(-m,m))
    return x
    pass
    # function that returns a list of n random numbers in span
    # -m, m with seed generator of random numbers (optional argument)
    
def find_max_gap(x):
    vmin <= x[0]
    dmax = 0
    for i in range (n):
        if (x[i] < vmin):
            vmin = x[i]
        elif (x[i] - vmin > dmax):
            dmax = x[i] - vmin
    return (dmax)
    
    # function that accepts a list of real numbers and returns the maximum distance between them
    pass

def present_list():
    print(" The random list is:",generate_random_floats(n,m,seed))
    # auxiliary function that prints the elements of a list
     pass

n=-1
while n < 0 or n == 0 :
    while True:
        try:
            # user input of how many random numbers
            n = int(input(">>Define the number (n) from the random numbers.\n-Positive number) :"))
            break
        except:
            continue
m=-1
while m < 0 :
    while True:
        try:
            # input user -m, m
            m = int(input(">>Define (range) from which random values will be selected.\n) :"))
            break
        except:
            continue
seed=0
b=0
x=0
s=0
while True:
    try:
        # input seed
        y = int(input("Enter the number that will set the seed value. :"))
        break
    except:
        continue
# create list of random numbers
generate_random_floats(n,m,seed)
present_list()
find_max_gap(x)   

当您在代码中定义 x = 0 时,您希望它将它传递给您的函数,因为它被隐含地视为全局变量。

然后在函数 generate_random_floats 中你认为你正在修改 x 而实际上那个函数中的 x 与你的 x 不同全球范围。

之后,在您调用 find_max_gap 期间,在行 vmin <= x[0] 中,x 仍然是一个整数(并且等于 0)。尝试像向量 (x[0]) 一样访问 x 会引发错误。

您的代码中需要修改一些内容,以便它执行您想要的操作:

  1. 我会将您的执行脚本封装在 if __name__ == "__main__": 中(有关详细信息,请参阅 What does if __name__ == "__main__": do?)。
  2. 我会避免以全局方式使用变量。我不会像这样调用你的函数 generate_random_floats(n,m,seed) 我会明确地使用函数的 return 来分配变量:x = generate_random_floats(n,m,seed).
  3. 以同样的方式,我将 x 作为 present_list() 函数的输入变量
  4. 我还会更正您对 seedy 的用法(在执行脚本和函数中)

如果您的代码太难理解,我稍后会提供更正。

编辑:更正代码

import random

def generate_random_floats(n,m,seed):
    x = []
    if (seed==0):  # modified y => seed
        random.seed()
        for i in range (n):
            x.append(random.uniform(-m,m))
    else:
        random.seed(y)
        for i in range (n):
            x.append(random.uniform(-m,m))
    return x
    # function that returns a list of n random numbers in span
    # -m, m with seed generator of random numbers (optional argument)
    
def find_max_gap(x):
    vmin = x[0] #corrected assignation
    dmax = 0
    for i in range (n):
        if (x[i] < vmin):
            vmin = x[i]
        elif (x[i] - vmin > dmax):
            dmax = x[i] - vmin
    return dmax
    # function that accepts a list of real numbers and returns the maximum distance between them


def present_list(x):
    print(" The random list is:", x) # replaced the function call with x
    # auxiliary function that prints the elements of a list


if __name__ == "__main__":
    n=-1
    while n < 0 or n == 0 :
        while True:
            try:
                # user input of how many random numbers
                n = int(input(">>Define the number (n) from the random numbers.\n-Positive number) :"))
                break
            except:
                continue
    m=-1
    while m < 0 :
        while True:
            try:
                # input user -m, m
                m = int(input(">>Define (range) from which random values will be selected.\n) :"))
                break
            except:
                continue
    seed=0
    while True:
        try:
            # input seed
            seed = int(input("Enter the number that will set the seed value. :")) # replaced y with seed
            break
        except:
            continue
    # create list of random numbers
    rfloats = generate_random_floats(n,m,seed) #generate new list rfloats
    present_list(rfloats) 
    mgap = find_max_gap(rfloats) 

    print("The max gap is :", mgap)