Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie here
#1
hi.i am struggling with my code because i dont get it why.
my code is:
def isPrime(x):
    """Returns whether or not the given number x is prime.

    A prime number is a natural number greater than 1 that cannot be formed
    by multiplying two smaller natural numbers.

    For example:
    - Calling isPrime(11) will return True
    - Calling isPrime(71) will return True
    - Calling isPrime(12) will return False 
    - Calling isPrime(76) will return False
    """

    list_of_prime=[]

    for i in range( 2,x):
        if (x%i==0):
            return False
        return True
    else:
        return False
                     
    return list_of_prime
x=int(input("please enter an integer: ") )      
prime=isPrime(x)
print(prime)
    
print([i for i in range (2,int(x**0.5)+1,2) if x%2==0])

prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23,
     29, 31, 37, 41, 43, 47, 53, 59,
     61, 67, 71, 73, 79, 83, 89, 97,
     101, 103, 107, 109, 113, 127,
     131, 137, 139, 149, 151, 157,
     163, 167, 173, 179, 181]

for i in prime_numbers:
    assert_true(isPrime(i), str(i) + ' is prime')

not_prime_numbers = [1, 8, 12, 18, 20, 27, 28, 30,
     42, 44, 45, 50, 52, 63, 66,
     68, 70, 75, 76, 78, 92, 98,
     99, 102, 138, 148, 150, 156, 158]

for i in not_prime_numbers:
    assert_true(not(isPrime(i)), str(i) + ' is not prime')

#test existence of docstring
assert_true(len(isPrime.__doc__) > 1, "there is no docstring for isPrime")
print("Success!")
my problem is i don t understand why does not print the strings
i get this error message:
Error:
Traceback (most recent call last): File "C:/Users/farka/AppData/Local/Programs/Python/Python38-32/isprime.py", line 38, in <module> assert_true(isPrime(i), str(i) + ' is prime') NameError: name 'assert_true' is not defined
can please somebody explain it to me where i make the mistakes and why.thank you
Yoriz write Oct-26-2022, 07:15 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
First, please post your code using the Python tags to preserve formatting. Otherwise very hard to see where your loops are, etc.

Where do you define assert_true? I do not see that function defined.

Also, remember when looking for primes, you only have to check odd numbers (with 2 being a special case) and you only have to go to the square root of the number in question.
Reply
#3
You don't have an assert_true() funciton. I used this:
def assert_true(condition, message):
    if condition:
        print(message)
If you are using unittest there is an assertTrue() function. Were you thinking of that? Sorting out assertTrue() fixes most of your errors.

Please clean things up before posting. Take some time to write the shortest runnable example you can that exhibits the problem you are having. Much of this code is gibberish, things that were left in from earlier designs that are not used in the code. Junk slowly accumulates and you occasionally need to do some housekeeping.
def isPrime(x):
    list_of_prime=[]   # Not used
 
    for i in range( 2,x):  # Is this an error?
        if (x%i==0):
            return False
        return True   # Is this an error?
    else:   # Never reached
        return False  # Never reached
                      
    return list_of_prime  # Never reached
Your test for primes is inefficient. Think about the problem before you start writing code. A couple of really simple performance boosts can be quickly added if you ask some questions:

Are there any even primes other than 2?
What is the maximum factor of a number?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020