Typeerror takes no arguments [closed]

2024/11/16 5:57:37

I know this may be a dumb question, but I am having a difficult time figuring out the error. I created a class called User to print out the first and last name, that's it. But when I try running it, it's giving me a typerror.

class User():def _init_(self, first_name, last_name):self.first_name = first_nameself.last_name = last_namedef describe_user(self):print("Your name is " + self.first_name.title() + ", " + self.last_name.title())
user_name = User('Andy', 'Wang')
user_name.describe_user()

The error is as follows:

File "c:\Users\Andy Wang\Documents\PCC\chap7.py", line 291, in <module>user_name = User('Andy', 'Wang')
TypeError: User() takes no arguments

I have made a similar program, but this time describing a restaurant and it works fine:

class Restaurant():def __init__(self, restaurant_name, cuisine_type):self.restaurant_name = restaurant_nameself.cuisine_type = cuisine_typedef describe_restaurant(self):print("\nThe restaraunt is called " + self.restaurant_name.title() + ".")print("It serves " + self.cuisine_type.title() + " food.")def open_restaurant(self):print(self.restaurant_name + " is open!\n")restaraunt = Restaurant('Szechuan Ichiban', 'Chinese')
restaraunt.describe_restaurant()
restaraunt.open_restaurant()

So I am just confused as to why the restaurant class works but my user class does not.

Thank you all for the help!

Answer

Its quite simple __init__ has 2 underscores before and at the end. So it should be

class User():def __init__(self, first_name, last_name):self.first_name = first_nameself.last_name = last_namedef describe_user(self):print("Your name is " + self.first_name.title() + ", " + self.last_name.title())
user_name = User('Andy', 'Wang')
user_name.describe_user()
https://en.xdnf.cn/q/118966.html

Related Q&A

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pip is not recognizing the install command (Windows 7, Python 3.3) [duplicate]

This question already has answers here:python3 --version shows "NameError: name python3 is not defined" [duplicate](2 answers)Closed 6 years ago.I am trying to install Python programs using P…

How do I check if 1 is always followed by a 0

In Python, I cannot find a solution as to how to determine whether there is always a 0 following a 1 somewhere in a list of numbers, to form a pair 10. It doesnt have to be direct follower.For clarity,…

Visual Studio Code debugs even when i run without debugging

i just installed VSCode and I used to work on it but now when I try to run without Debugging with Ctrl + F5. it seems to opens up python debug console and debug like below image enter image description…

Mock global function call while importing

Suppose I have a file called a.py with code likeimport mod1 mod1.a()def b():print("hi")Now if I want to mock fun b() then unittest.py while have import statement at top likefrom a import bat …

can the order of code make this program faster?

Hi this is my first post, I am learning how to write code so technically I am a newbie.I am learning python I am still at the very basics, I was getting to Know the if statement and I tried to mix it …

How can I label a node that is the initial vertex in a cycle from graph data

I need to implement an algorithm such that in a collection of unique and ordered graph edges, I can find a cyclic node. E.g. for a ->b, b->c, c->a, then a is a cyclic node and thus I want to a…

Pandas : How to drop #DIV/0! and NA values in new column in pandas dataframe?

I did some calculation and have #DIV/0! in my dataframe. How to drop these values and count further ? I followed df.dropna but dataframe still counting #DIV/0!. Please suggest.df.insert(loc=df.column…

unsupported operand for 3 instances of two classes and one method?

Im trying to get the program to take the hp stat from enemyUnit, the attack stat from unit, and the damage stat from tackle and put them into one math problem in the method getHit(). this is the code:…