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
Just playing around on codewars and came across this problem: https://www.codewars.com/kata/5514e5b77e6b2f38e0000ca9/train/python
Now I built this function (in Sublime):
def up_array(arr):
is_valid = []
if len(arr) > 0:
is_valid.append(1)
else:
is_valid.append(0)
for x in arr:
if x < 0:
is_valid.append(0)
else:
is_valid.append(1)
if 0 not in is_valid:
arr = int(''.join(str(n) for n in arr))
arr += 1
arr = str(arr)
arr = [int(n) for n in arr]
return arr
else:
return None
print(up_array([4,3,2,5])
and I keep getting SyntaxError: unexpected EOF while parsing
BUT when I run the same code outside the function:
arr = [4,3,2,5]
is_valid = []
if len(arr) > 0:
is_valid.append(1)
else:
is_valid.append(0)
for x in arr:
if x < 0:
is_valid.append(0)
else:
is_valid.append(1)
if 0 not in is_valid:
arr = int(''.join(str(n) for n in arr))
arr += 1
arr = str(arr)
arr = [int(n) for n in arr]
print(arr)
else:
print(None)
The code runs great through various tests. Any thoughts as to what the issue with the function is?
I have tried re-tabbing & re-writing but when I put code inside a function. I started getting this parsing error.
I don't want to put too much time into the issue but it will linger in the back of my mind so any thoughts would be greatly appreciated!
SyntaxError: unexpected EOF while parsing error is caused because of missing parenthesis on last line print(up_array([4,3,2,5])
It should be like this:
print(up_array([4,3,2,5]))
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 5 months ago.
Improve this question
I'm getting the syntax error, that return is outside the func. I have rewritten the code but the problem persists. What am I doing wrong?
def prime_1(mn):
if mn < 2:
return 0
prime_in = [2]
x = 3
while x <= mn:
for y in prime_in:
if x % y == 0:
x += 2
break
else:
prime_in.append(x)
x += 2
print(prime_in)
return len(prime_in)
I'm voting to close this question, but since the edit queue is full...your issue is simply syntax. Format your code correctly and you are done:
def prime_1(mn):
if mn < 2:
return 0
prime_in = [2]
x = 3
while x <= mn:
for y in prime_in:
if x % y == 0:
x += 2
break
else:
prime_in.append(x)
x += 2
print(prime_in)
# returns in function now
return len(prime_in)
Check the indentation of your return statement. The first statement has too much whitespaces in front of it.
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
I am writing a recursion function that simply returns x^n:
def myPow(x,n) -> float:
def recurs_n(x,n):
if n==1:
return x
if n>1:
result = myPow(x,n-1)
return result
return recurs_n(x,n)
print(myPow(2,5))
The output that I am getting for the above is 2, which isn't correct obviously. Any idea where I am going wrong?
Your inner function calls the outer function, that should not be allowed. Furthermore, there is no logic applied anywhere for the multiplication when going to the n-1 step. So when n>1 you need to return x*myPow(x,n-1)
I should also mention, that in your case there is no real need to have an inner and outer function, one function should be enough. Like so;
def myPow(x,n) -> float:
if n == 1:
return x
if n > 1:
return x*myPow(x,n-1)
print(myPow(2,5))
Your code runs until power n=5 reaches 1, where it activates the first if condition where x is returned (here x=2) without continuing onwards.
An alternative is presented below:
def power(N, P):
# if power is 0 then return 1
if P == 0:
return 1
# if power is 1 then number is
# returned
elif P == 1:
return N
else:
return (N*power(N, P-1))
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
I didn't understand this thing:
list1 = [34,2,1,3,33,100,61,1800]
for n in list1:
try:
n%2 == 0
print(n)
except:
pass
The output of the code above shows every number, but I need just even numbers.
What is my mistake?
I suspect you are missing a basic idea somewhere. try / except is used for catching errors. In order to use this in the way you are trying to, you need to cause an error under some condition. One easy way is to assert the number is even. assert n % 2 == 0 says if n is not even, raise an exception. Then you can catch the exception and skip it with a pass
list1 = [34,2,1,3,33,100,61,1800]
for n in list1:
try:
assert n % 2 == 0 # cause an error if `n` is not even
print(n)
except AssertionError:
pass
You were missing a condition if
list1 = [34,2,1,3,33,100,61,1800]
for n in list1:
try:
if n%2 == 0:
print(n)
except:
pass
Two things. You forgot an if statement before preforming n%2
Second thing is, what is the point of the try and except. You don't need it.
Below is an example of your code in
list comprehension.
list1 = [34,2,1,3,33,100,61,1800]
even = [n for n in list1 if n%2 == 0]
output
[34, 2, 100, 1800]
simplified code
list1 = [34,2,1,3,33,100,61,1800]
even = []
for n in list1:
if n%2 == 0:
even.append(n)
output
[34, 2, 100, 1800]
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
Why am I getting a MemoryError when my code is only 13 lines long? Could it be because I could potentially be getting a very long list?
I am trying to return a list of a sequence of numbers here (the list will be x and then the numbers obtained until 1). The sequence will be complete when the variable val reaches 1.
def sequence(x):
my_list = [x]
val = 0
while (val!= 1) and (x != 1):
if x % 2 == 0:
val = x / 2
my_list.append(val)
else:
val = 3 * x + 1
my_list.append(val)
return my_list
print(sequence(6))
This is the error message I'm getting:
Traceback (most recent call last):
File ******, line 12, in <module>
print(sequence(6))
File ******, line 7, in sequence
my_list.append(val)
MemoryError
How can I get rid of this error?
In your code, you are not manipulating x. So, x will always be 6.
And as x will always 6, your val will never be equal to 1 in both cases
val=x/2 #will result in val=3
And also
val=3*x+1 #will result in val=19
So, both conditions ( val != 1 ) and ( x != 1 ) will always True and your loop will not terminate at all.
This infinite loop goes on filling your list and eventually you will get memory error.
Hope you got it.
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
Building a stock data scraper for Finviz. I am using Spyder3 installed via Anaconda for Python 3.7.
My code is below. When I execute the x = 0, and then x = x+1 code line by line in the terminal, it works just fine. When I run entire script, I getthe same error if I use x += 1 or x = x + 1.
def finviz_query(tickerlist):
'''Get's source code from FinViz and Creates a List of Lists for Export '''
url="https://finviz.com/screener.ashx?v=140&t=" + str(stocks)
response = requests.get(url)
source=response.text
soup = bs4.BeautifulSoup(source)
priceLST = [i.get_text() for i in soup.find_all('a')]
del priceLST[0:37]
del priceLST[len(priceLST)-2:len(priceLST)]
stockLST = re.split(',',stocks)
stock_outputLST = []
while len(priceLST) > 0:
if priceLST[0] in stockLST:
stock_outputLST.append([priceLST[0:16]])
del priceLST[0:16]
if len(priceLST) < 1:
break
x = 0
while x < len(stock_outputLST):
if x < len(stock_outputLST):
stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")
x = x + 1
else:
break
stock_outputLST[len(stock_outputLST)-1][0].append('0')
stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))
The error output is here:
...:stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))
...: return print('finviz_query complete')
File "<ipython-input-114-81b306b1a6de>", line 22
x = x + 1
^
SyntaxError: invalid syntax
Thanks in advance!
Whenever you get an error like this that doesn't quite make sense, look at the line above:
stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")
You're missing a closing parenthesis ) at the end.