#🔒 How can I fix this program? Its supposed to filter through 2-49 and return only prime number

251 messages · Page 1 of 1 (latest)

lament idol
#
def prime_or_not(num):
    count = 0 
    for l in range(2, num-1):
        if num%l == 0:
            count +=1
    if count == 0:
        return True
    else:
        return False
all_number_list = list(range(1,50))
def get_prime_number(num):
   empty_list = []
   for number in all_number_list:
      number =num 
      if prime_or_not(number) is True:
         empty_list += number
         print(empty_list)

  
get_prime_number(8)

It supposed to pick the index of the prime number entered... as long as itis prime to begin with

tidal runeBOT
#

@lament idol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lament idol
#

I've started with defining function prime_or_not which I will use to iterate through the range of numbers to find T or F if they are prime

#

1 true

#

the 2nd function then is designed to simply intake a prime number.. or not prime number and return what the index of that prime number is

#

so get it...

#
def prime_or_not(num):
    count = 0 
    for l in range(2, num-1):
        if num%l == 0:
            count +=1
    if count == 0:
        a =  1
    else:
        a = 2
all_number_list = list(range(1,50))
def get_prime_number(num):
   empty_list = []
   for number in all_number_list:
      number =num 
      if a = 1 :
         empty_list += number
         print(empty_list)

  
get_prime_number(8)
#

I have altered the code due to instability

runic torrent
#

I don’t thin you can access the a variable from the first function in the second function.

#

What’s the error you are facing now?

lament idol
#

U mean a?

runic torrent
#

yeah

#

if a = 1:

#

also it should be ==

lament idol
#

NameError: name 'a' is not defined

lament idol
runic torrent
#

Yes that’s because you can’t access it without calling the first function

#

You should add a return statement in the first function

#

and then doing a = prime_or_not()

#

To get the value of a in the first function

#

yeah it’s too hard to code on phone to show you

#

also for the first function, you never called it too, so even though you wrote it, it has no use

lament idol
#

a = prime_or_not(8)

def prime_or_not(num):
    count = 0 
    for l in range(2, num-1):
        if num%l == 0:
            count +=1
    if count == 0:
        return a ==  1
    else:
        return a == 2
all_number_list = list(range(1,50))
def get_prime_number(num):
   empty_list = []
   for number in all_number_list:
      number =num 
      if a == 1 :
         empty_list += number
         print(empty_list)
runic torrent
#

what does the input of 8 supposed to mean/do?

lament idol
#

8 depends on the function

#

at the moment.. 8 should return 2

#

as its not prime

runic torrent
#

You are checking for a range of numbers from 2-49 right

#

then honestly the first function’s input should be the list containing all numbers from 2-49

#

then you check for the prime numbers

#

and appending to a empty list

lament idol
#

one function for getting the prime number

#

2nd one could just grab it

runic torrent
#

Getting the prime number means you’re going to input number one by one from 2 to 49? Just to check if it’s prime?

lament idol
#

but i dont think in the current set up 8 and a list [1,2,3,4,5,6] could be replaced

lament idol
#

returning the index ofit

runic torrent
#

I have no idea what’s going on, maybe wait for next one

lament idol
#

first function establishes the input the 2nd function

#

which just tells you where to grab that prime number

#

i.e. 3 is the 2nd prime

#

1 is the 1st

lament idol
#

print(list(range(1,2)))
def prime_or_not(num):
    count = 0 
    for l in list(range(1, num+1)):
        if num%l == 0:
            count +=1
    if count == 0:
        return True
    else:
        return False
prime_or_not(1)


#

not sure why this won't work

#

I jeep getitng false

#

oooohhugj

#

erwqa

#

hi

safe siren
#

@lament idol count is 1

lament idol
#

i had it working before

lament idol
safe siren
#

step through it in your head

#

!e ```py
print(list(range(1, 1 + 1)))

tidal runeBOT
#

@safe siren :white_check_mark: Your 3.12 eval job has completed with return code 0.

[1]
safe siren
#

that's your list you're iterating though

lament idol
#

yeah thats good.

#

for l in list(range(1, num+1)):

safe siren
#

so l is 1 there

lament idol
#

if num%l == 0:

safe siren
#

1 % 1 == 0

#

so count increments

lament idol
#

wait 2 is also prime

safe siren
#

count is always 1

#

2 isn't being tested at all

#

you're passing 1

lament idol
#

yes but i change the funciton

#

prime_or_not(3)

safe siren
#

well I'm not psychic

lament idol
#

print(list(range(1,2)))
def prime_or_not(num):
    count = 0 
    for l in list(range(1, num+1)):
        if num%l == 0:
            count +=1
    if count == 0:
        return False
    else:
        return True
prime_or_not(3)
#

see... its still outputting true

safe siren
#

it was returning false a minute ago

#

and you said you didn't understand why

lament idol
#

i think if count > len(list)?

#

Alright it works now

#

print(list(range(1,2)))
def prime_or_not(num):
    count = 0 
    for l in list(range(1, num+1)):
        if num%l == 0:
            count +=1
    if count<len(list(range(1,num+1))):
        return False
    else:
        return True
prime_or_not(2)



#

Now I need to exchange the input of a simple integer into a list of integers from 1-50

safe siren
#

You should start the range with 2

lament idol
#

To create a list of prime numbers

lament idol
safe siren
#

because all your numbers are divisible by 1

lament idol
#

yes but the conditional could also be count <= num

#

<=

safe siren
#

Also your function is going to be inefficient for big values of num since you're creating a list

#

Why not just use the range?

sage mountain
lament idol
#

nvm its not

safe siren
#

There are far better ways to test for primality.

lament idol
sage mountain
#

you can iterate through a range

lament idol
#

or are u saying i could achieve the same thing if

safe siren
#

you iterate through a range

lament idol
#

what's the datatype of a range?

sage mountain
#

range

#

!e print(type(range(3, 9)))

tidal runeBOT
#

@sage mountain :white_check_mark: Your 3.12 eval job has completed with return code 0.

<class 'range'>
lament idol
#

lol

#

ok

sage mountain
#

also why did you do count < len(list(range(1, num + 1)))?

safe siren
#

¯_(ツ)_/¯

lament idol
#

Is 3 not a prime number...

sage mountain
#

it is a prime number

#

1 is not a prime number tho

lament idol
#

damn my function still not 100%

#

def prime_or_not(num):
    count = 0 
    for l in range(1, num+1):
        if num%l == 0:
            count +=1
    if count<len(range(1,num+1)):
        return False
    else:
        return True
prime_or_not(3)


sage mountain
#

please fix this count<len(range(1,num+1))

lament idol
#

oh wait... 1 is not... i thought a prime number was divisible by 1 and itself...

#

so prime is 1,2,3,5,7,13

sage mountain
#

which you could then just put in return count < num

lament idol
sage mountain
#

did someone already mention how the range of the for loop can be better?

lament idol
#

def prime_or_not(num):
    count = 0 
    for l in range(1, num+1):
        if num%l == 0:
            count +=1
    return count < num

prime_or_not(4)
sage mountain
#

why include 1 and num in the range?

lament idol
#

if its divisible by 2 or num -1 then its good

sage mountain
#

what?

#

4 is not prime

lament idol
#

ik there is an error for usre

#

it gives true

sage mountain
lament idol
#

oh count == 2?

#

means prime

sage mountain
#

that's not what you said

#

you said "if it's divisible by 2"

#

that would mean num % 2 == 0

lament idol
#

``py

#

def prime_or_not(num):
count = 0
for l in range(1, num-1):
if num%l == 0:
count += 1
if count ==2:
return count == num

prime_or_not(4)```

sage mountain
#

no that's... what?

lament idol
sage mountain
#

I have no idea what you are saying

lament idol
#

def prime_or_not(num):
    count = 0 
    for l in range(1, num
):
        if num%l == 0:
            count += 1
    if count ==2:
        return count == 2/True



prime_or_not(5)
#

1,2,3,4,5...

#

itterateing through 1, giuves a count increment

#

5 also does

#

sorry i fixed it @sage mountain

sage mountain
#

it seems like you are just making random guesses

lament idol
#

def prime_or_not(num):
    count = 0 
    for l in range(1, num):
        if num%l == 0:
            count += 1
    if count ==2:
        return True


prime_or_not(5)
#

Idk why this doesn't work.

#

Can u explain this super quickly?

#

🙂

#

Idk if its the indexes

#

I mean what else do you want to include in there expect num

safe siren
#

Maybe you could explain why you think it would work.

lament idol
#

range(1,num)

#

Yes. please

#

I thik that would help greatly and i would apprecite your explanation on why it doesn't actually fruit

#

so count = 0 ,

safe siren
#

No, you explain.

lament idol
#

l in the range (1, the inputted number)

safe siren
#

What do you think is happening and what do you expect to happen?

lament idol
#

so in this case 5

#

so l takes the form of 1 to begin with

#

all the way up to 5

#

1,2,3,4,5

#

on each iteration

#

if the num %l == 0: we increment

#

every list generated includes the 1 and the number itself so we know the modulus will be zero twice

#

hence count = 2 for prime numbers

#

this doesn't work for number 1 tho... since only 1

sage mountain
#

works fine for 1

#

1 is not prime

lament idol
#

i like to think 1 is prime.. but ok... 2 is definitely prime

#

(1,2)

#

i think the indexing might be wrong

#

becauselogically i think the codeflows?

#

maybe it should be

sage mountain
lament idol
#

for l in range(len(range(1,num)))

#

def prime_or_not(num):
    count = 0 
    for l in (range(1, num)):
        if num%l == 0:
            count += 1
    if count ==2:
        return True


print(prime_or_not(2))
sage mountain
lament idol
#

lol

#
def prime_or_not(num):
    count = 0 
    for l in (range(1, num+1)):
        if num%l == 0:
            count += 1
    if count ==2:
        return True
    else:
        return False


print(prime_or_not(11))
#

works now

#

is there a problem with the following???

#
def prime_or_not(num):
    count = 0 
    for l in range(2, num-1):
        if num%l == 0:
            count +=1
    if count == 0:
        return True
    else:
        return False
prime_or_not(8)
sage mountain
#

yes, num - 1 is wrong for the 0 checking

#

should be num

lament idol
#

wdym???

#

ok

#

num-1

sage mountain
#

range(2, num - 1) --> range(2, num)

lament idol
#

thanks

#

should it be (1,num)

#

or range is like the index

sage mountain
#

no

#

??

lament idol
#

list[0:2] --> list [ start exactly 0,: end before 2]

sage mountain
#

that's slicing

lament idol
#

yes but analogise it here

sage mountain
#

range does not include the stop value

#

!e

print(*range(3, 7))
lament idol
#

thanks

tidal runeBOT
#

@sage mountain :white_check_mark: Your 3.12 eval job has completed with return code 0.

3 4 5 6
lament idol
#
def prime_or_not(num):
    count = 0 
    for l in (range(1, num+1)):
        if num%l == 0:
            count += 1
    if count ==2:
        return True
    else:
        return False


def get_prime(n):
    empty_list
    for a in range(1,50):
        prime_or_not(a)
        if a:
            empty_list += a
get_prime(1)
#

What can I do here?

#

Second function currently has a mess variable..

#

def get_prime(n):
    empty_list = []
    for a in range(1,50):
        prime_or_not(a)
        if a:
            empty_list += n

get_prime(1)
sage mountain
lament idol
#

u mean the function call?

sage mountain
#

yes it's doing nothing

lament idol
#

I'm hoping to cancel out all non-prime

sage mountain
#

because you calculate a value but don't use it

lament idol
#

ahhh

#

its the same as saying like

#

len(list)

#

type(len(list))

sage mountain
#

?

lament idol
#

without having print

#

print(type...

sage mountain
#

!e more like

x = 1
x + 3
print(x)
tidal runeBOT
#

@sage mountain :white_check_mark: Your 3.12 eval job has completed with return code 0.

1
lament idol
#
def get_prime(n):
    empty_list = []
    for a in range(1,50):
        b = prime_or_not(a)
        if b:
            empty_list += list(n)
get_prime(1)
sage mountain
#

rather, move the function call to the if

sage mountain
#

like, why did you suddenly put list(n)

lament idol
#

Im new to coding!

#

TypeError: 'int' object is not iterable

#

it did say this.

#

so i changed to list

sage mountain
#

and it will give the same error

#

except in a new spot

lament idol
#

should i try this.

#

str_concat = ",",join(a)

sage mountain
#

why...

lament idol
#

if its prime

#

then put it in a str, ,

#

separated by commas.

sage mountain
#

for what reason?

lament idol
#

TypeError: can only join an iterable

#

I want a list

#

oof prime numbers 1-49

#

using 49 function calls.

sage mountain
#

I am not here to teach you the absolute basics of Python

lament idol
#

but i dont know what the input of this variable should be

#

but i suppose at this moment its the list... 1-49

#

😄

#

I feel like im on the dip

#

but i will resurface and make exponential gains!

#
def get_prime(n):
    empty_list = []
    numbers =  range(1,50)
    for a in numbers:
        b = prime_or_not(a)
        if b:
            str_concat = ",".join(a)
get_prime(1)
#

Omg I got an output

#

its2

#

Now i got 47

#

Which i think is telling me 47 iterations of this thing

#

True

#
def get_prime(n):
    empty_list = []
    numbers =  range(1,50)
    for a in numbers:
        b = prime_or_not(a)
        if b:
            str_concat = a
    return str_concat
get_prime(1)
#

I mean if i can get 47 then i can concat 47 times.. but

sage mountain
#

I regret wasting my time here. Please have the courtesy of not making random guesses for the next person who tries to help you.

tidal runeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.