noazet abdo
noazet abdo

Reputation: 113

invalid syntax in printing prime lists in python

quys i am totally new to programming so please help me ... i am trying to make a function produce all the prime numbers and put it in list and generate a ranodm number from this list ... here is my code

from random import choice
question_3():
    list = []
    for i in range(2,20):
        flag=True
        for num in list:
            if(i%num==0):
                flag=False
        if(flag):
            list.append(i)
            p = choice(list)
        print list , p 

question_3()

but an error appeared

SyntaxError: invalid syntax

Upvotes: 0

Views: 111

Answers (3)

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

You are missing a def before the function name:

def question_3():
    for i in range(2,20):
        #...

Upvotes: 2

scandinavian_
scandinavian_

Reputation: 2576

from random import choice

def question_3():
    list = []
    for i in range(2,20):
        flag=True
        for num in list:
            if(i%num==0):
                flag=False
        if(flag):
            list.append(i)
            p = choice(list)
        print list , p 

question_3()

Upvotes: 1

Matthias
Matthias

Reputation: 13222

You missed the def before the definition of the function question_3.

Some additional comments: Please get rid of the unnecessary parens when you use an if-statement and don't use list as a variable name because you shadow the builtin list.

Reading PEP-8, the Style Guide for Python Code, might be a good idea.

Upvotes: 1

Related Questions