vec.append不起作用,它告诉我列表仍然为空

vec.append not working, it tells me the list is still empty

提问人:Nano Casas 提问时间:8/24/2022 更新时间:8/24/2022 访问量:24

问:

我在这里的第一篇文章,我正在研究一个项目,我需要将 CARGAR(n, vec) 中的数据附加到 vec 中,我尝试过附加,但它不起作用!我需要帮助。我错过了什么吗? 我随机选择时间,并制作一个名为 suma_t 的平均值,然后我制作一个名为 carrera 的变量,其中包含所有数据的 str 版本。

import random


def mostrar_menu():
    print("-------------------------------------------------------------------------")
    print(" MENU ")
    print("1. Cargar ")
    print("2. Listar ")
    print("-------------------------------------------------------------------------")


def validar_positivo():
    n = int(input("Cuantos corredores hay?: "))
    while n <= 0:
        print("ERROR! Numero invalido")
    return n


def cargar(n, vec):
    for i in range(n):
        numero = str(i+1)
        nombre = str(input("Ingrese el nombre del corredor " + numero + ": "))
        tiempo_1 = random.randint(0, 60)
        tiempo_2 = random.randint(0, 60)
        tiempo_3 = random.randint(0, 60)
        suma_t = tiempo_1 + tiempo_2 + tiempo_3
        tiempo_t = suma_t/3
        print("Su tiempo promedio es de", tiempo_t)
        carrera = str(nombre), str(tiempo_1), str(tiempo_2), str(tiempo_3), str(tiempo_t)
        vec.append(carrera)
    main()


def ordenar(vec):
    n = len(vec)
    for i in range(0, n-1):
        for j in range(i+1, n):
            if vec[i].tiempo_t < vec[j].tiempo_t:
                vec[i], vec[j] = vec[j], vec[i]


def main():
    vec = []
    a = 0
    while a != 3:
        mostrar_menu()
        a = int(input("Ingrese su opcion: "))
        if a == 1:
            n = validar_positivo()
            cargar(n, vec)
        if a == 2:
            ordenar(vec)
            if not vec:
                print("Llene la lista")
            else:
                print(vec)
        if a == 3:
            print("Adios!")


if __name__ == "__main__":
    main()
python-3.x 列表 vec

评论

0赞 quamrana 8/24/2022
可疑的是,调用.这将创建一个新的 .也许不应该叫main。cargar()main()vec
0赞 Nano Casas 8/24/2022
你是对的!这修复了它。

答: 暂无答案