Aaron Merrifield
Aaron Merrifield

Reputation: 13

Python: syntax assignment error in iteration

I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.

x = 0
y = 2
counter = x
integer = y
while (counter>999):
      if (y%2 == 0 or y%3 == 0):
          y = y + 1
      else:(counter = counter + 1 and integer = integer + 1)
print (y)    

when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario

Upvotes: 0

Views: 91

Answers (3)

DoronS
DoronS

Reputation: 347

In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.

so you need to split the statements.

x = 0
y = 2
counter = x
integer = y
while (counter>999):
    if (y%2 == 0 or y%3 == 0):
        y = y + 1
    else:
        counter += 1
        integer += 1
print (y)

Upvotes: 0

In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:

x = 0
y = 2
counter = x
integer = y
while (counter>999):
    if (y%2 == 0 or y%3 == 0):
        y = y + 1
    else:
        counter += 1
        integer += 1
print (y)    

Upvotes: 2

gefei
gefei

Reputation: 19776

try this

   else:
      counter = counter + 1
      integer = integer + 1

Upvotes: 0

Related Questions