Python syntax error - python

When I run this code in python, it is giving me a syntax error and highlighting the "l" in the line that I have pointed out. Why is it doing this?
from Euler import primeSieve,sumDig
def powerSieve(n):
powers = []
primes = primeSieve(100)
for i in primes:
j = 2
while i ** j <= n:
for k in primes:
if i == k and i ** j >= 10: powers.append(i ** j)
else:
l = 1
while l * (i ** j) <= n:
if l * (i ** j) >= 10: powers.append(l * (i ** j)
##THIS LINE l *= k
j += 1
return sorted(set(powers))
from time import clock
start = clock()
print "Answer to PE119 = ",powerSieve(100)
elapsed = clock() - start
print elapsed * 1000,"ms"

You're missing a close bracket on the line before. Change this:
if l * (i ** j) >= 10: powers.append(l * (i ** j)
To this:
if l * (i ** j) >= 10: powers.append(l * (i ** j))

You forgot to close the brackets on the preceding line. It should be:
powers.append(l * (i ** j))

You have an if statement above that line, so the above line should end in a colon and the highlighted line should be indented.

Related

How do i convert the Maclaurin Series for tan x equation to python code?

I'm trying to calculate the nth term but its giving me wrong answers
import math
def bernoulli(m):
if m == 0:
return 1
else:
t = 0
for k in range(0, m):
t += math.comb(m, k) * bernoulli(k) / (m - k + 1)
return 1 - t
def pn(n, x):
sum = 0
for i in range(n):
sum += ((bernoulli(2 * i)) / math.factorial(2 * i)) * (-4**i) * (1 - (4**i)) * (x**((2 * i) - 1))
Equation:
Here are a few comments:
In python, the convention is to include the start, and exclude the end. list(range(1,4)) is only [1, 2, 3], not [1,2,3,4]. Thus your Bernouilli loop should be for k in range(0, m+1) and your pn loop should be for i in range(1, n+1).
Exponentiation has a higher precedence than most operators. -4**i is parsed as -(4**i), not as (-4)**i.
sum is already the name of a builtin function in python. It is very strongly advised not to shadow the names of builtins. Call that variable s or total or something else, not sum.
Finally, the code becomes:
import math
def bernoulli(m):
if m == 0:
return 1
else:
t = 0
for k in range(0, m+1):
t += math.comb(m, k) * bernoulli(k) / (m - k + 1)
return 1 - t
def pn(n, x):
s = 0
for i in range(1, n+1):
s += ((bernoulli(2 * i)) / math.factorial(2 * i)) * ((-4)**i) * (1 - (4**i)) * (x**(2 * i - 1))
return s
And, using builtin function sum:
import math
def bernoulli(m):
if m == 0:
return 1
else:
return 1 - sum(math.comb(m, k) * bernoulli(k) / (m - k + 1)
for k in range(0, m+1))
def pn(n, x):
return sum((bernoulli(2 * i)) / math.factorial(2 * i)) * ((-4)**i) * (1 - (4**i)) * (x**(2 * i - 1)
for i in range(1, n+1))

I need help doing this double summation in python

I'm pretty new in python and I'm having trouble doing this double summation.
I already tried using
x = sum(sum((math.pow(j, 2) * (k+1)) for k in range(1, M-1)) for j in range(N))
and using 2 for loops but nothing seens to work
You were pretty close:
N = int(input("N: "))
M = int(input("M: "))
x = sum(sum(j ** 2 * (k + 1) for k in range(M)) for j in range(1, N + 1))
It also can be done with nested for loops:
x = 0
for j in range(1, N + 1): # [1, N]
for k in range(M): # [0, M - 1]
x += j ** 2 * (k + 1)
After a little math...
x = M * (M+1) * N * (N+1) * (2*N+1) // 12

Python 3.7 does not support assignment expressions

I have the following code:
n = int(input())
a, b, c = map(int, input().split())
result = sum(s // c + 1 for i in range(n) for j in range(n - a * i) if (s := n - a * i - b * j - 1) >= 0)
print(result)
But I have an error that Python 3.7 does not support assignment expressions in this part (s := n - a * i - b * j - 1). How can I rewrite it? I want to rewrite it to python3.7
The simple, though repetitive, fix is to "inline" the value of s.
result = sum((n - a * i - b * j - 1) // c + 1
for i in range(n)
for j in range(n - a * i) if n - a * i - b * j - 1 >= 0)
Start with converting the generator expression to plain code and then it is a simple task:
result = 0
for i in range(n):
for j in range(n - a * i):
s = n - a * i - b * j - 1
if s >= 0:
result += s // c + 1

How to print a triangle in python?

I want to make a function to print triangle like the following picture. User can insert the row number of the triangle. The total lenght of first row is must an odd.
I try to use below code :
def triangle(n):
k = 2*n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i+1):
print("* ", end="")
print("\r")
n = 5
triangle(n)
Here is the expected output image :
and here is my actual output image :
but I can't remove the star middle star. And it's not Upside - Down Triangle
You could try a different way.
def triangle(n) :
for i in range(1,n+1) :
for j in range(1,i) :
print (" ",end="")
for j in range(1,(n * 2 - (2 * i - 1))
+1) :
if (i == 1 or j == 1 or
j == (n * 2 - (2 * i - 1))) :
print ("*", end="")
else :
print(" ", end="")
print ("")
n = 5
triangle(n)
Not sure how cool implementation is this but it gives results:
def triangle(n):
print(' '.join(['*']*(n+2)))
s = int((n/2)+1)
for i in range(s):
star_list = [' ']*(n+2)
star_list[-i-2] = ' *'
star_list[i+1] = '*'
print(''.join(star_list))
n = 5
triangle(n)
Output:
* * * * * * *
* *
* *
*
for n = 7:
* * * * * * * * *
* *
* *
* *
*
I would try a recursive solution where you call the printTriangle() function. This way, it will print the point first and move it's way down the call stack.

Printing empty Pyramid

I want to write a program that prints this output:
*
* *
* *
* *
*
But it prints this instead:
*
* *
* * *
* * * *
* * * * *
Suppose n = 5 is the input from the user, then the first star should be in the centre, and the ending star as well, so I can divide the input number by 2 to get the position of the first and the last star.
For the rest of the stars I am not understanding how to make it so that if one star is above then the next star should not be below it.
def Empty_triangle(n):
k = 2*n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
Empty_triangle(n)
How about this sort of thing?
print("\n".join([" "*(n-2-i)+"*"+" "*(2*i-1)+("*"if i>0 else"") for i in list(range(n-1))+[0]]))
Which, for example, for n=5, gives this output:
*
* *
* *
* *
*
Is this the kind of thing you had in mind?
Here's a less code-golfish version
def Empty_triangle(n):
for i in list(range(n-1))+[0]: #i.e. [0,1,2,3,4,0] so the last line is the same as the first
line = ""
leadingSpaces = n-2-i
line += " "*leadingSpaces
line += "*"
if i != 0:
middleSpaces = 2*i-1
line += " "*middleSpaces
line += "*"
print(line)
# Driver Code
n = 5
Empty_triangle(n)

Categories

Resources