python script to give a list of prime no.

Barry Scott barry at barrys-emacs.org
Sun Apr 5 09:28:21 EDT 2020



> On 5 Apr 2020, at 14:08, Sathvik Babu Veligatla <sathvikveligatla at gmail.com> wrote:
> 
> On Sunday, April 5, 2020 at 6:09:04 PM UTC+5:30, Chris Angelico wrote:
>> 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
> 
> yeah bro, tried as said. But still no luck... :-(
> i cannot understand the issue in the code.

You need to figure out what the code is doing.

One way to do this is to add some more print statements to
show the values of your variables as the code runs.

What is the value of i for example in the for loop?
What does the list l look like?
Also try printing the values that you use the if statments.
print( l.count(1) == 0) etc.

Barry


> -- 
> https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>


More information about the Python-list mailing list