![]() |
Error in code NameError: name ' ' is not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Error in code NameError: name ' ' is not defined (/thread-29700.html) Pages:
1
2
|
Error in code NameError: name ' ' is not defined - ppman00 - Sep-16-2020 Hi. I have a problem with code(self studying-just started). I have a problem "NameError: name 'numb' is not defined". It should be easy solution for that and im sitting with it about 2 hours. my code: from typing import List, Any, TextIO lista: List[int] = [] file = str(input("file name: ")+'.txt') n = int(input("give how many digits u want to input, modulo 4 must be 0: \n", )) while n % 4 != 0: n = int(input("wrong", )) print("give " + str(n) + " digit") while len(lista) < n: digit= int(input("give digit: ")) if not 0 <= digit <= 9: print('wrong') else: lista.append(digit) print(lista) m = int(len(lista)/4) #print(m) for i in range(0, m): x = i y = i + 1 numb.x=int(str(lista[0 + i*4]) + str(lista[2 + i*4])) numb.y=int(str(lista[1 + i * 4]) + str(lista[3 + i * 4])) numb.i= (numb.x) + (numb.y) chr(a.i) print(chr(a.i), end=" ") file: TextIO = open((file), 'w') file.write(chr(a.i)) file.close() RE: Error in code NameError: name ' ' is not defined - Jeff_F - Sep-16-2020 Try declaring the variable numb.. that should fix it. I tried the code and here's where I put it. #print(m) numb = 0 for i in range(0, m): x = i y = i + 1 numb.x=int(str(lista[0 + i*4]) + str(lista[2 + i*4])) numb.y=int(str(lista[1 + i * 4]) + str(lista[3 + i * 4])) numb.i= (numb.x) + (numb.y) chr(a.i) print(chr(a.i), end=" ") RE: Error in code NameError: name ' ' is not defined - ppman00 - Sep-16-2020 I've already tried that. After running the program it work till that line.
RE: Error in code NameError: name ' ' is not defined - ppman00 - Sep-16-2020 I just found the way to solve the problem. On the begining i've addes import numbNot sure if it is the rigt way to do it. Do you have other solutions? RE: Error in code NameError: name ' ' is not defined - Jeff_F - Sep-16-2020 Did you try it out? I tried out the fix I suggested and I didn't get an error message. RE: Error in code NameError: name ' ' is not defined - micseydel - Sep-16-2020 (Sep-16-2020, 09:06 PM)Jeff_F Wrote: Did you try it out? I tried out the fix I suggested and I didn't get an error message.What was the exact code you tried? RE: Error in code NameError: name ' ' is not defined - ppman00 - Sep-16-2020 I used this code below. It program works till the line with numb.x. In 1st stage program asking for file name. (it will save the result there) In the next stage program asks how many digits your going to input, and that and modulo 4 from that number must be equal 0. Third stage is input digits. Till that moment it works. After that i receive error from typing import List, Any, TextIO lista: List[int] = [] file = str(input("file name: ")+'.txt') n = int(input("give how many digits u want to input, modulo 4 must be 0: \n", )) while n % 4 != 0: n = int(input("wrong", )) print("give " + str(n) + " digit") while len(lista) < n: digit= int(input("give digit: ")) if not 0 <= digit <= 9: print('wrong') else: lista.append(digit) print(lista) m = int(len(lista)/4) #print(m) numb = 0 for i in range(0, m): x = i y = i + 1 numb.x=int(str(lista[0 + i*4]) + str(lista[2 + i*4])) numb.y=int(str(lista[1 + i * 4]) + str(lista[3 + i * 4])) numb.i= (numb.x) + (numb.y) chr(numb.i) print(chr(numb.i), end=" ") file: TextIO = open((file), 'w') file.write(chr(numb.i)) file.close() RE: Error in code NameError: name ' ' is not defined - micseydel - Sep-16-2020 What do you intend for that line to do? numb is an int . ints don't have an x attribute (or y, or i). You can't just add that field to a random int. I would tell you what to do instead, but again, I'm not sure.
RE: Error in code NameError: name ' ' is not defined - Clunk_Head - Sep-16-2020 As others have said, you created numb as an int and are then trying to define variables inside of it. There are at least two simple solutions: 1) Define a variable for each case. #Instead of numb.x = 0 #use numb_x = 02) Use a dictionary numb = {} numb['x'] = 0 numb['y'] = 0 RE: Error in code NameError: name ' ' is not defined - Jeff_F - Sep-17-2020 (Sep-16-2020, 09:11 PM)micseydel Wrote:(Sep-16-2020, 09:06 PM)Jeff_F Wrote: Did you try it out? I tried out the fix I suggested and I didn't get an error message.What was the exact code you tried? I simply added a line of code to initialize numb to zero. It got past the error and it seems to work fine., #print(m) numb = 0 for i in range(0, m): x = i y = i + 1 numb.x=int(str(lista[0 + i*4]) + str(lista[2 + i*4])) numb.y=int(str(lista[1 + i * 4]) + str(lista[3 + i * 4])) numb.i= (numb.x) + (numb.y) chr(a.i) print(chr(a.i), end=" ") Here's the results after I initialize numb to zero. I have no idea what the code is supposed to do but it works up to the this point. Some comments would be nice to let us know what you're trying to accomplish., Quote:Traceback (most recent call last): |