Python invalid Syntax Why?c [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 4 years ago.
Improve this question
Why is this invalid syntax?
if 0.9*x < d[o] < 1.1*x:
Here's the whole code
def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)

The line you quoted isn't the source of your error. This line is:
x = (500/(2**(i))*10**-6
Note the mismatched parentheses.

def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6 # Here is a SyntaxError, Because You've started 3 parentheses but terminated only 2. So, add a closing parenthesis in the right place.
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)
See the comment

Related

Python - function that creates boolean term if modulo operator = 0 [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 4 months ago.
Improve this question
I have to create a function that returns true if b is a divisor of a.
I haven't done anything with functions yet.
I made this:
def is_divisor(a,b):
a % b = i
if i > 0:
return False
if i = 0:
return True
is_divisor(10,5)
It should show true, but it doesn't.
The error in your code is on the line if i = 0: it should be if i == 0. To check for equality, use ==.
You can also simplify this function to simply:
def is_divisor(a, b):
return a % b == 0
Try this:
def is_divisor(a, b):
try:
remainder = a % b
except ZeroDivisionError:
return False
return remainder == 0
You should always check if you're dividing by 0! Otherwise your function is going to raise an exception.

SyntaxError: invalid syntax "if stack [-1].pop () != brackets [stack [-2].pop ()]:" [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
Does anyone know why I'm getting this syntax error?
n = len (stack [-1])
for i in range (n -1):
stack [-1].append (stack [-2].pop ()
if stack [-1].pop () != brackets [stack [-2].pop ()]:
balance = False
elif stack == [[], []]:
balance = True
if stack [-1] == [] and stack [-2] == []:
stack.pop()
if stack [-1].pop () != brackets [stack [-2].pop ()]:
^
SyntaxError: invalid syntax
You're missing a ) in the end of line 4:
stack [-1].append (stack [-2].pop ()
Should be:
stack [-1].append (stack [-2].pop ())

Trying to count certain characters in python [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 5 years ago.
Improve this question
I want the code to count ", !, ?, , but when I run the code it counts all characters typed. Could anyone tell me where I messed up?
def how_eligible():
total = 0
x = ('"','!','?',',')
y = raw_input('Write your essay here.')
for y in x:
if y in x:
total = total + 1
print total
Incase want to know.. A Pythonic 1 liner solution
x = ('"','!','?',',')
y = input('Write your essay here.')
len([i for i in y if i in x])
I would change your double for loop as
for c in y:
if c in x:
total = total + 1
You can also use Counter from the collections module:
from collections import Counter
in_s = 'abc?c?"!'
need = ['"', '!', '?']
char_count = Counter(in_s)
for c in need:
print(c, char_count[c])

variance of list values [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 5 years ago.
Improve this question
I want to calculate the variance of values in list x1. Could anyone fix the error in this code?!
def my_mean(L):
s = 0
for i in range(0, len(L)):
s = s + L[i]
return s / len(L)
def my_var(L):
t = 0
for i in range(0, len(L)):
t = t + L[i] - def my_mean(L)
return t*t / len (L)
x1 = [1, 3, 4, -3, 8]
v1 = my_var(x1)
print(v1)
You need to use the def keyword only when you define the function.
When you call to the function you don't need to use def again.
Fix this row:
t = t + L[i] - def my_mean(L)
To:
t = t + L[i] - my_mean(L)

Counting even numbers in a given list [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'm trying to count the numbers in a given list and only count the even numbers. I keep getting a syntax error and don't know what the issue is.
x = [1,5,4,7,2,10,8,19,27,26,54,80]
def count_evens(g_list):
y = 0
for i in g_list:
if g_list[i] % 2 = 0:
y = y + 1
else:
y = y + 0
print(str(y))
count_evens(x)
The syntax error is coming from if g_list[i] % 2 = 0: What's wrong about my syntax?
Thanks!
syntax error
You want to compare so use == not = (single equal is for assignment)
if g_list[i] % 2 == 0:
index is out of range
To loop through all elements of the list, you can use this form:
for i in g_list:
if i % 2 == 0: # No need for g_list[i]
# in your for loop,
# i is an element from the list, not an index
g_list[i] % 2 = 0 is an assignment statement (and an illegal one at that since you "Can't assign to an operator"). Assignment statements are not allowed in if statements (only expressions).
You want g_list[i] % 2 == 0 which is a logical expression.

Categories