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.
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 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.
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 last year.
Improve this question
For example:
The if statement would take input from the user and would run the correct function for it.
if x == 1 or 2 or 3:
do this or that
Your current statement will always be true. It checks if x == 1 which may be true or false, then it checks the value 2 which is true, because all nonzero integers are "truthy". (And similarly for 3)
The correct way to check a single variable against multiple values is to use:
if x == 1 or x == 2 or x == 3:
Or, you could check if x is in a collection of values. This is usually the preferred way if there are lots of possible values to check.
if x in {1, 2, 3}:
This uses a set literal, but you could use any collection type such as a list or a tuple instead.
If you need to take a different action depending on the value of x, you can use if, elif, else
if x == 1:
# do something
elif x == 2:
# do something different
else:
# x did not match any of your values so raise an error or something
yes, you would want to do
if x == 1 or x == 2 or x == 3:
...
or you could do
if x in [1 ,2]:
...
in 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 1 year ago.
Improve this question
I’m trying to make a fizzbuzz program: count up to n; for each i up to n, print “fizz” if a multiple of 3, and “buzz” if a multiple of 5 — if a multiple of both, print “fizzbuzz”.
I’m using modular arithmetic, but for some reason my syntax is wrong.
Here is my code (without the “fizzbuzz” bit yet):
def fizzbuzz(n):
for i in range(n):
if i % 3 = 0
return fizz
if i % 5 = 0
return buzz
else
return i
print(fizzbuzz(100))
Error Code:
Python3IDE(Python 3.7) running!
File "/var/mobile/Containers/Data/Application/FD2AF249-3788-42B7-90B2-929E9D35A2E1/Documents/FizzBuzz.py", line 5
if i % 3 = 0
^
SyntaxError: invalid syntax
Pytho3IDE run end!
Any help is much appreciated.
The problem is in the if i % 3 = 0 and the if i % 5 = 0. The comparison operator, in this case, would be ==, so you would have to rewrite both statements with the comparison operator. Currently, you are using the assignment operator, which Python doesn't understand.
Your code is correct, you just missed out on some of the syntax.
def fizzbuzz(n):
for i in range(n):
Note the double equal signs instead of single equal sign. A single equal sign is an assignment operator, while a double is a comparison operator.
if i % 3 == 0:
return fizz
if i % 5 == 0:
return buzz
else:
return i
print(fizzbuzz(100))
your syntax is wrong because you are using = instead of == to check is equal:
if i % 3 = 0
return fizz
if i % 5 = 0
return buzz
should be:
if i % 3 == 0:
return "fizz"
if i % 5 == 0:
return "buzz"
= is an assignment operator while == is equality operator.
Also, you forgot to add colon : at the end of each if statement.
And, you forgot to use " " on fizz and buzz. Python will search for variables with that name and won't find them. you want to return a string with fizz or buzz.
You should consider taking one of the hundred tutorials for beginners in python, that there is on YouTube...
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'm writing a program that takes a list of floating point numbers representing distance in inches and converts them to shoe sizes to the nearest 0.5.
I'm trying to compare an empty list and expect the output to be The list is empty. Instead I don't return any value. Not sure where I'm going wrong?
foot_length = []
for length in foot_length:
if length == 0:
print('The list is empty.')
else:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)
I've tried a number of different approaches none of which get the output I want/expect.
if not length:
print('The list is empty.')
if len(length) == 0:
print('The list is empty.')
Try to change your code to
foot_length = []
if len(foot_length) == 0:
print('The list is empty.')
else:
for length in foot_length:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)
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