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...
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 2 months ago.
Improve this question
it is my first post in StackOverflow I'm having trouble understanding and applying this exercise on my own with python please could help me!
getting this TypeError: 'NoneType' object is not iterable
This is the exercise Algorithm:
Definition: An integer is said to be perfect if it is equal to the sum of all its divisors. Examples: 6 and 28 are perfect since
6 = 1+2+3 (knowing that 1, 2 and 3 are the divisors of 6 less than 6)
28= 1+2+4+7+14 (knowing that 1, 2, 4, 7 and 14 are the divisors of 28 less than 28)
1) Create a function liste_divisors(), which accepts an integer N as a parameter and returns the list of its divisors less than N (1 included).
2) Make a function is perfect(), which accepts a (positive) integer N as a parameter and returns “True” if it is perfect and “False” otherwise (use the function from the 1st question)
3) Create a Perfect List() function, which accepts a Limit parameter, then returns a list containing the perfect numbers less than Limit
This is my attempt so far,
but there are errors, please help me to correct these errors and make it better
def liste_diviseur(N):
for i in range(1,N):
if(N%i == 0):
print(i)
def est_parfait(M):
s = 0
for i in liste_diviseur(M):
s += i
if(s == M):
return True
else:
return False
def liste_parfait(Limite):
if(Limite<est_parfait(Limite)):
return Limite
m = int(input('Giving an number :'))
print(liste_parfait(m))
As you iterate over the result of liste_diviseur() this latter function must return an iterable (something we can iterate).
It can be a sequence : a list or a tuple.
def liste_diviseur(N):
diviseurs: list[int] = []
for i in range(1,N):
if(N%i == 0):
print(i)
diviseurs.append(i)
return diviseurs
Or simpler you can use a Generator.
def liste_diviseur(N):
for i in range(1,N):
if(N%i == 0):
print(i)
yield i
Consider adding N itself to the divisors list.
N divided by N equals 1.
You can add N at the end of the loop.
Or you can have a higher range with range(1, N+1) as the upper bound is not comprised.
"1) Create a function liste_divisors()....and returns the list of its divisors.."
Your function just prints the divisors, You need to collect them in a list and return it.
Then you can iterate over the list in est_parfait(M).
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Help, I'm trying to create a function that uses modulus to print numbers that are divisible by an integer, in this case, multiples of 17:
def nextDivisible(n):
if n % 17 == 0:
print n
else:
nextDivisible(n+1)
But the output is for example:
n: 93 next divisible: 102
None
n: 59 next divisible: 68
None
Why is there a None?! and how do I remove it, thanks!
I think your problem is that you are printing out the result instead of returning it from the function.
If you change your code to be like this it should fix it:
def nextDivisible(n):
if n % 17 == 0:
return n
return nextDivisible(n + 1)
How this works with recursion is that your function nextDivisible is going to be repeatedly called until it reaches its base case of n being divisible by 17. When that case occurs it will return n all the way back up the recursive stack to where the original first call to the function was done.
You also don't need an else statement because return statements break out of the function code. But sometimes else's are good for readability.
You don't need to:
print(nextDivisible(n))
I think you get None printed because your function doesn't return anything. It is also redundant to print the return of the function because you print the numbers inside the loop.
All you need is:
nextDivisible(n)
And it will print the divisible numbers.
If you are use nextDivisible() in other functions, it may be worth nothing that a return statement may be required depending on the nature of how you are using it.
For all intents and purposes this should work just fine.
And python 3.x:
x = [x for x in range(1000)]
def nextDivisible(x, mod=17):
c = 0
for n in x:
if n % mod == 0:
print(n)
c += 1
else:
#nextDivisible(n+1)
pass
print('{} numbers divisible by {} in {}'.format(c, mod, len(x)))
nextDivisible(x)
Expected output:
59 total numbers divisible by 17 in 1000
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am making a text to binary translator and needed to make my x go up by increments of one. But when I do this it wont let me. Here is the part python does not like,
if beep == 1:
if x < length:
x + 1 = x
x + 1 = x is an invalid expression in python (invalid in almost all other programming languages).
x = x + 1 will be a valid increment.
You need to see first how Assignment Operator works.
Assigns values from right side operands to left side operand.
When updating variables, the variable by itself needs to be on the left of the assignment operator. So instead of x + 1 = x, use x = x + 1 or simply x += 1
you cant do operations in left hand side. so x + 1 = x is invalid in python.
You can do this by: x = x + 1 or x += 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 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.