简体   繁体   中英

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

You are missing a def before the function name:

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

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM