python script to give a list of prime no.

Chris Angelico rosuav at gmail.com
Sun Apr 5 08:38:37 EDT 2020


On Sun, Apr 5, 2020 at 10:26 PM Sathvik Babu Veligatla
<sathvikveligatla at gmail.com> wrote:
>
> hi,
> I am new to python, and i am trying to output the prime numbers beginning from 3 and i cannot get the required output.
> It stops after giving the output "7" and that's it.
>
> CODE:
> a = 3
> l = []
> while True:
>     for i in range(2,a):
>         if a%i == 0:
>             l.append(1)
>         else:
>             l.append(0)
>
>     if l.count(1) == 0:
>         print(a)
>         a = a + 2
>     elif l.count(1) >> 0:
>         a = a + 2
>
>

Firstly, good job on several points: you posted your code, you said
what the purpose is, and you said how the output differs from that.
Unfortunately not everyone does that :)

Secondly, though: What does "some_number >> 0" mean? Play around with
that in the interactive interpreter and see what results you get.

You may want to consider using a simple 'else'. When you have exactly
two possibilities, you don't need to say "else, if". For instance,
suppose you say "If today is a weekday, go to work; otherwise, play
games", you don't need to word it as "otherwise, if today is a
weekend, play games" - you just say "otherwise". You can simplify this
code the same way.

Something worth remembering is that all code can be buggy, so writing
less code usually means you have less bugs. Try to write your program
with less code rather than more. :)

All the best!

ChrisA


More information about the Python-list mailing list