jeudi 22 juillet 2021

why value of f not changing to 1 when even number is provided?

p=[]
l=[]
v="run"
a=int(input("enter num or end: "))
while v!="end":
    l.append(int(a))
    a=input("enter num or end: ")
    v=a  
for a in l:
    f=0
    for j in range(2,a//2):
         if a%j == 0:
            f=1
            break
    if f==0:
        p.append(a)

here I am inputting numbers and if its a prime number then I am putting it to list 'p' and at last output p. i cant understand why 1st if statement not working when I am providing an even number like in picture below 4 is inputed so it should have changed value of f to 1 so that it doesn't get into list 'p' but its not. i am very new to python so there is possibility of silly mistakes.

enter image description here on left code, on right output

Aucun commentaire:

Enregistrer un commentaire