NameError: name 'user_record' is not defined - python

I am running this code in Python 3.7.
record = ('Dave', 'dave#example.com', '773-555-1212', '847-555-1212')
name, email, *phone_numbers = user_record
Error:
Traceback (most recent call last):
File "C:\UsersPythonWizard\Desktop\Python\Python.py", line 2, in <module>
name, email, *phone_numbers = user_record
NameError: name 'user_record' is not defined
>>>

You defined the tuple as record, not user_record, so user_record is not defined. Change one variable to the other and you'll be good.

Related

Getting the TypeError: 'list' object is not callable in Python

class Student:
def __init__(self,first,last,id):
self._first_name = first
self._last_name = last
self._id_number = id
self._enrolled_in = []
def enroll_in_course(self,course):
self._enrolled_in.append(course)
return self._enrolled_in()
s1 = Student("kathy","lor","323232")
s1.enroll_in_course("hello")
print(s1._enrolled_in)
In the code above, i am getting the error as:
Traceback (most recent call last):
File "main.py", line 14, in
s1.enroll_in_course("hello") File "main.py", line 10, in enroll_in_course
return self._enrolled_in()
TypeError: 'list' object is not callable
I am trying to solve the error, but unable to do so. Can anybody help me here.
You have defined self._enrolled_in but you're adding it to self.enrolled_in
Missed the underscore (self._enrolled_in.append)
You have called the attribute _enrolled_in in your __init__() method. In the enroll_in_course() method you're trying to append to enrolled_in which does not exist. So try by adding the underscore in front.
You are missing an _ in the appended statement. You should write self._enrolled_in.append(course) on your enroll_in_course method.

What are TypeErrors? One of them appeared in my code

Why is this error coming? It is a TypeError. And also, what are TypeErrors anyways?
All the code needed:
from collections import namedtuple
account = namedtuple('person', 'password')
acc1 = account('example', 'passwordex1')
And here is the error:
Traceback (most recent call last):
File "C:\Users\user\Desktop\Python\Secret.py", line 4, in <module>
acc1 = account('example', 'passwordex1')
TypeError: __new__() takes 2 positional arguments but 3 were given
namedtuple('person', 'password') creates a type person with one field (password). You want this instead:
account = namedtuple('account', ('person', 'password'))

NameError creating instance of imported class

I have a subclassed Course class as follows:
# course.py
class Course:
"""Represent a single Course"""
kind = 'Gen Ed'
def __init__(self, name, number) :
self._name = name # 'private' instance variable\
self._number = number
self.__display()
def display(self):
print(self.kind,"Course:" , self._name, self._number, sep=" ")
__display = display # private copy
class CSCourse(Course):
"""Represent a single CS Course"""
kind = 'CS' # class variable shared by all CSCourses
def __init__(self, name, number, language, numberOfPrograms):
Course.__init__(self, name, number)
self._language = language
self._numberOfPrograms = numberOfPrograms
def display(self):
Course.display(self)
print('Language', self._language,
'Number Of programs:', self._numberOfPrograms, sep = ' ')
I import the module as follows:
>>>from course import *
This does not throw any exception, but then when I issue the following to call the constructor, I get the error below?
>>> cs360=CSCourse("Special Topics", 360, "python", 21)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'CSCourse' is not defined
What am I doing wrong please? I did also try to see what methods are available in the classes imported. It seems nothing is being imported!
>>> import inspect
>>> inspect.getmembers(Course, lambda a:not(inspect.isroutine(a)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Course' is not defined
>>> inspect.getmembers(CSCourse, lambda a:not(inspect.isroutine(a)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'CSCourse' is not defined
For anyone else having this problem, check if you have circular imports (in file a.py from b import * and in file b.py from a import *). Python doesn't seem to raise an exception when that happens, but the import doesn't work. Restructuring the code to remove the circular import fixed the problem for me.

suggesting a new name for Image file & save it with PIL img.save()

Is there any way to suggest a new name or get a string supplied from the commandline like: ./py_module.py <img_file> <new_file_name>
.
.
.
new_img = img.resize((horz_rsz, w_input), Image.ADAPTIVE)
name_input = input("suggest a name: ") // or sys.argv[2]
print(im_width, im_height, wh_ratio, horz_rsz, new_img.size)
new_img.save(name_input+'', 'png')
I'm getting error because the .save() function for an image searches for that name if it's defined in program scope.
so, is this possible?
errors:
height to resize: 500
suggest a name: abc
Traceback (most recent call last):
File "img_resize.py", line 17, in
name_input = input("suggest a name: ")
File "", line 1, in
NameError: name 'abc' is not defined

Can't call function inside function in exec() environment

I have some python (2.7) code using exec() :
import math
def some_function(a, b):
return a+b
safe_dict = { 'Sqrt': math.sqrt, 'Smurf': some_function, "__builtins__": None }
with open('my_file.py') as f:
exec(f.read(), safe_dict, {})
print('The End')
And "my_file.py" is:
print('in exec script')
print('sqrt(2) = %f' % Sqrt(2)) # Call to math.sqrt through name 'Sqrt' given in globals
print('3+4 = %d' % Smurf(3, 4)) # Call to some_function through name 'Smurf' given in globals
def my_func(x):
return Sqrt(2+x)
print("my_func: %f" % my_func(1)) # No problems
def my_func2(x, f):
return f(x-1)
print("my_func2: %f" % my_func2(5, my_func)) # No problems too
def my_func3(x):
return my_func(x-1) # Here, leads to a "NameError: global name 'my_func' is not defined" in the next line
print("my_func3: %f" % my_func3(5))
I don't understand why there is a NameError in my_func3 when it tries to call my_func.
Why my_func3 is not able to call my_func, even if previously defined ?
Is there a way to make it work (not with my_func defined in the main module) ?
edit
Error is:
Traceback (most recent call last):
File "main.py", line 9, in <module>
exec(f.read(), safe_dict, {})
File "<string>", line 16, in <module>
File "<string>", line 15, in my_func3
NameError: global name 'my_func' is not defined

Categories

Resources