[Python] Cannot get a list to be iterated. I'm sing a program that cannot import the NumPy...

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have used the following code:

    import math
    import decimal
    def calculate_c(a, h):
    return (1 * 15 * (4 * (h ** (3/2))) ) / (3 * (a ** (1/2)))
    def calculate_d(a, h, u):
    return 2 * ((u * h) - ((1/3) * a * (u ** 3)))
    def calculate_F(a, u):
    return ((math.arcsinh(2 * abs(a) * u) / (4 * abs(a))) + ((u * np.sqrt((4 * (a ** 2) * (u ** 2)) + 1)) /2))
    def calculate_g(F):
    return 2 * F * 1.24 * 0.5 * 0.2 * 15
    # Definiëren van zoekbereik
    def range(x, y, jump):
    while x < y:
    yield float(x)
    x += decimal.Decimal(jump)

    h_values = list(range(0, 150, '0.1'))[-1] # h moet kleiner zijn dan 15
    a_values = range(0, 100, '0.1') # a mag niet te klein zijn om te voorkomen dat sqrt(h/a) < 7.5


    max_c = 0
    best_h, best_a = None, None
    for h in h_values:
    for a in a_values:
    u = math.sqrt(h / a)
    if u < 7.5:
    d = calculate_d(a, h, u)
    if d <= 25:
    c = calculate_c(a, h)
    if c > max_c:
    max_c = c
    best_h, best_a = h, a

    best_u = math.sqrt(best_h / best_a)
    best_d = calculate_d(best_a, best_h, best_u)
    best_F = calculate_F(best_a, best_u)
    best_g = calculate_g(best_F)

    print(f'Maximale waarde van c: {max_c}')
    print(f'Optimale waarde van h: {best_h}')
    print(f'Optimale waarde van a: {best_a}')

    print(f'Waarde van u: {best_u}')
    print(f'Waarde van d (dwarsdoorsnede): {best_d}')
    print(f'Waarde van F: {best_F}')
    print(f'Gewicht van de boot (g): {best_g}')


    Final prints and explanations with # are in Dutch, this is not important. When I try to run it, I get the following error:


    Traceback (most recent call last):
    File "", line 25, in
    TypeError: 'float' object is not iterable
    ​

    It says it cannot iterate float, but I also tried printing it and it only returned one value. The lines for generating the lists came from this site, but I am not very good at coding and struggle to integrate it into my function. Is there any way to resolve this?

    I need the values to be floats.

    Continue reading...

Compartilhe esta Página