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?
Related
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 4 years ago.
Improve this question
The program i am working on have a class with constructor defined as follow :
def Oracle(object) :
Agold = None
sentence = None
def __init__(self, sentence, Agold):
self.Agold = Agold
self.sentence = sentence
but, when i call the constructor in my main method, as follow :
oracle = Oracle(words, ref_tree)
python 3 give me this error :
Traceback (most recent call last):
File "oracle_test.py", line 52, in test_exemple
oracle = Oracle(words, ref_tree)
TypeError: Oracle() takes 1 positional argument but 2 were given
i don't understand the origin of this problem, and i don't see what gone wrong.
Can someone give me an explanation ?
Thanks
You defined Oracle as a function instead of a class. Use class instead of def. Also, assuming Agold and sentence are supposed to be instance variables instead of class variables, Agold = None and sentence = None are not needed (see this).
class Oracle(object):
def __init__(self, sentence, Agold):
self.Agold = Agold
self.sentence = sentence
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 5 years ago.
Improve this question
I did a DFS solution for fun, but what has been bugging me is that normally if you have an if...and statement, Python will stop checking after the first statement is False. In this case I get an index out of range if I put the two statements together.
For example, this works fine
if not nums:
if s[i]=='I' and (path[i]<path[i+1]) or (s[i]=='D' and path[i]>path[i+1]):
ret=min(ret, path)
But I will get an error for this:
if not nums and if s[i]=='I' and (path[i]<path[i+1]) or (s[i]=='D' and path[i]>path[i+1]):
ret=min(ret, path)
Can anyone spot something I don't see?
class Solution(object):
def findPermutation(self, s):
"""
:type s: str
:rtype: List[int]
"""
def dfs(ret,nums,i,path):
if not nums:
if s[i]=='I' and (path[i]<path[i+1]) or (s[i]=='D' and path[i]>path[i+1]):
ret=min(ret, path)
for j in xrange(len(nums)):
if len(path)>1:
if (s[i]=='I' and path[i]>path[i+1]) or (s[i]=='D' and path[i]<path[i+1]):
return ret
ret=dfs(ret,nums[:j]+nums[j+1:],i+1,path+[nums[j]])
else:
ret=dfs(ret,nums[:j]+nums[j+1:],i,path+[nums[j]])
return ret
return dfs([len(s)+3],range(1,len(s)+2),0,[])
It seems this is just a syntax error using an extra if, use:
if not nums and
instead of
if not nums and if
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
single_char = {}
for item in lines:
for index in range(len(item)):
if item[index - 1] in space:
if item[index + 1] in space:
if item[index] in alplist:
char = item[index]
single_char[char] = index
return single_char
else:
continue
else:
continue
else:
continue​
This is my function by for some reason Python keeps telling me that the return statement "return single_char" is outside of the function. Can somebody please explain how i can fix this problem.
There is no function. And return statement should be always inside a function.
Use def keyword to define a function in python.
Example:
def sum(a, b):
return a+b
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
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 9 years ago.
Improve this question
regardless of what the functions have to return, my code seems to not call the first function properly, as when I try to pass some doctest it raises the error:
File "preg3.py", line 27, in mesDivisions
if nombreDivisions(llista[0],m)>=nombreDivisions(llista[1],m):
NameError: global name 'nombreDivisions' is not defined
here is my code:
def nombreDivisons(n,m):
x=0
def aux(n,m):
if n<m:
return x
else:
if n%m==0:
x=x+1
return aux(n/m,m)
else:
return x
def mesDivisions(llista,m):
if len(llista)==1:
return llista[0],nombreDivisions(llista[0],m)
else:
if nombreDivisions(llista[0],m)>=nombreDivisions(llista[1],m):
del llista[1]
return mesDivisions(llista,m)
else:
del llista[0]
return mesDivisions(llista,m)
any ideas why?
Check your white space. You want at least one and according to pep8 two blank lines between functions.
You failure is a typo though. It should be nombreDivisions but you left out the i so it is nombreDivisons.