[Python] NameError: name 'i' is not defined. How to solve this error? [closed]

Discussão em 'Python' iniciado por Stack, Novembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    When i run this code

    from math import *
    import matplotlib.pyplot as plt

    N = 10
    p0 = 0.8

    p_dist = [factorial(N) / (factorial(N - k) * factorial(k)) * p0 ** k * (1 - p0) ** (N - k) for k in range(N + 1)]
    # print(p_dist)
    s = [sum(p_dist[:i + 1]) for i in range(len(p_dist))]
    print(sum(p_dist))
    print("i, p_dist, s")
    for k in range(N + 1):
    print(k, p_dist, s)

    bar_width = 1
    opacity = 0.8
    rects1 = plt.bar(range(N + 1), p_dist, bar_width,
    alpha=opacity,
    color='b', )
    plt.show()


    I get the error

    ---------------------------------------------------------------------------
    NameError Traceback (most recent call last)
    Cell In[53], line 13
    11 print("i, p_dist, s")
    12 for k in range(N + 1):
    ---> 13 print(k, p_dist, s)
    15 bar_width = 1
    16 opacity = 0.8

    NameError: name 'i' is not defined


    I don't understand the error , I just begin my python course.

    How can I solve this error?

    Continue reading...

Compartilhe esta Página