What is wrong with this _init_ [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What is wrong with this python code? I have been trying to learn how to use _init_ but can't get it to work
class Giraffes:
def _init_(self, spots):
self.giraffe_spots = spots
ozwald = Giraffes(100)
print(ozwald.giraffe_spots)

You need to use two underscores before and after:
def __init__(self, spots):
You only used one on either side. When incorrectly spelled, it won't be called when creating a new instance.
Demo:
>>> class Giraffes:
... def __init__(self, spots):
... self.giraffe_spots = spots
...
>>> ozwald = Giraffes(100)
>>> print(ozwald.giraffe_spots)
100

Related

invalid syntax in a class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
There's a task that I need to complete and there's a problem:
solution.py:1:22: E999 SyntaxError: invalid syntax
Code of the class will be in next paragraph but here's code that will use my class:
from solution import OddEvenSeparator
separator = OddEvenSeparator()
separator.add_number(1)
separator.add_number(5)
separator.add_number(6)
separator.add_number(8)
separator.add_number(3)
print(' '.join(map(str, separator.even())))
print(' '.join(map(str, separator.odd())))
I've created a class looking like this:
def OddEvenSeparator:
def __init__ (self):
self.evening = []
self.theOdds = []
def add_number(self, number):
if number % 2 == 0:
self.evening.append(number)
else:
self.theOdds.append(number)
def even(self):
return self.evening
def odd(self):
return self.theOdds
It must add a number with .add_number to each list depending on if it is an even or odd. Then, when user uses .even or .odd the class returns one of two lists, evening or theOdds but compilator gives me solution.py:1:22: E999 SyntaxError: invalid syntax. online-python.com says about this problem too pointing at :. What's wrong?

Why is my function running but not giving any output? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
def func(*args,**kwargs):
if 'fruit' in args and 'food' in kwargs:
print('I would like to eat {} {}'.format(args['fruit'], kwargs['food']))
func(fruit='apple',fruit1='banana',food='biryani',dinner='meat')
*args is used to pass to pass a non-key worded argument list. Try this -
def func(*args,**kwargs):
if len(args)>0 and 'food' in kwargs:
print('I would like to eat {} {}'.format(args[0], kwargs['food']))
func('apple',fruit1='banana',food='biryani',dinner='meat')
check out this stackoverflow question for more details on *args and **kwargs.

How can I know the outcome of the "return True" function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
In the following code, if I run it on IDLE, it gives me no outcome.
But with the command return True, wasn't supposed to give me True on the outcome?
Or do I have to add something like print True?
data = [2,4,5,7,8,9,12,14,17,19,22,25,27,28,33,37]
target = 28
#Linear Search
def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return True
return False
Only defining the function is not enough you also need to call it.
In your case so add following:
print(linear_search(data,target))

Constructor return me None type instead of class type [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've a problem with python 3, classes and contructor. I've my class:
class Menu:
def __init__(self, store):
self.store = store
# other code
and when I create a object
menu = Menu(store)
the variable menu is None type, instead of being Menu type.
Can anyone help me, please?
Psychic debugging: Assuming you haven't replaced Menu somewhere else with something completely different, you defined a __new__ on Menu in your # other code, and you failed to return a newly created object from __new__; thus, __new__ returns None, and you don't actually construct anything.
Typically, you don't need both __new__ and __init__ on user defined classes in Python; just do all the initialization work in __init__ and get rid of __new__.

function NameError not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to python and I get this error in my script below:
NameError: name 'csv_name_for' is not defined
def cvs_name_for(date):
return "abuse_{0}.csv".format(date)
def export_to_csv(complaints, date):
with open("{0}/{1}".format(CSV_DIR, csv_name_for(date)), "w") as csvfile:
# do stuff
What is wrong?
Your function name is cvs... and you call csv....
Fixed:
def csv_name_for(date):
return "abuse_{0}.csv".format(date)
def export_to_csv(complaints, date):
with open("{0}/{1}".format(CSV_DIR, csv_name_for(date)), "w") as csvfile:
# do stuff

Categories