简体   繁体   中英

Python nameerror: name weight not defined

I've written a short code of a simple neural net:

T=input("enter T: ")

for i in range(int(T)):
    N=input("enter N: ")
    minX=input("enter minX: ")
    maxX=input("enter maxX: ")
    for j in range(int(N)):
        weight[j]=input("enter weight: ")
        bias[j]=input("enter bias: ")
    x=minX
    nonspammer=0
    spammer=0
    for k in range(maxX-minX+1):
        for l in range(N):
            x=x*w[l]+b[l]
        if x%2==0:
            nonspammer+=1
        else:
            spammer+=1
        x+=1
    print(nonspammer,spammer,sep=" ")

This code is giving me the error:

Nameerror: name weight not defined

Could someone help me find out the reason for this error? I'm a beginner to coding in python.

Before for j in range(int(N)): add the lines

weight=[0 for i in range(N)]
bias=[0 for i in range(N)]

You are trying to access elements of these lists even before declaring them to be lists

You can't get the index of a non-exiting list, so you have to create first like :

for i in range(int(T)):
    N=input("enter N: ")
    minX=input("enter minX: ")
    maxX=input("enter maxX: ")
    weight=[0]*int(N)
    bias=[0]*int(N)
    for j in range(int(N)):
        weight[j]=input("enter weight: ")
        bias[j]=input("enter bias: ")

or create it empty and append it :

for i in range(int(T)):
    N=input("enter N: ")
    minX=input("enter minX: ")
    maxX=input("enter maxX: ")
    weight=[]
    bias=[]
    for j in range(int(N)):
        weight.append(input("enter weight: "))
        bias.append(input("enter bias: "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM