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.
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 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
My code is below, but I'm getting this weird output for my inner product code and don't know why it's not calculating the correct inner product. nums and nums2 ask the user for an equal list of numbers in which the inner product will be calculated. Any assistance would be appreciated.
def innerproduct(nums, nums2):
sum = 0.0
for i in range(len(nums)):
sum += nums[i] * nums2[i]
return innerproduct
The error arising because of the return innerproduct statement since that is the name of the function.
Instead, did you mean to return the sum?
def innerproduct(nums, nums2):
sum = 0.0
for i in range(len(nums)):
sum += nums[i] * nums2[i]
return sum
That's not "weird output", it's exactly what you told it to return. You ignored the result and returned a reference to the function object.
Try this instead:
result = 0
for i in range(len(nums)):
result += nums[i] * nums2[i]
return result
Note: do not give a variable the same name as a built-in type or function.
You can do this more directly with the built-in sum function:
return sum(nums[i] * nums2[i] for i in range(len(nums)))
Or perhaps even better:
return sum(a * b for a, b in zip(nums, nums2[i]))
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 years ago.
Improve this question
It's a simple question of recursive function, which extracting every digits from a positive number. As the comment mentioned, I have known the mistake is because of global variable, but I still have no idea to fix it to get expected result. Thanks.
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5,1,2,0]
But the expected output should be
print(getdigits(120)) # expected output = [1,2,0]
print(getdigits(5)) # expected output = [5]
You are using the existing LIST that already have values from the previous function call.
First clear the list and then try to call for another value.\
Try it
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120))
list_1.clear() #clear list values
print(getdigits(5))
def getdigits(n):
global list_1
if n == 0:
listTemp = list_1
list_1 = []
return listTemp
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5]
you need to declare list 1 as a global varible inside your function so you can clear list one within th function
i have tested it and got that output
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
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.