Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:42:06+00:00 2026-06-07T12:42:06+00:00

I’m trying to learn Python by working through the problems on the Project Euler

  • 0

I’m trying to learn Python by working through the problems on the Project Euler website. I know exactly what I want my code to do, and my method works on paper, but I can’t make the code work.

GitHub link: https://github.com/albyr/euler-python/blob/master/euler3.py

I have created two functions, one which works out the factors of the target number, and one which checks whether a given number is prime.

# Function that finds all the factors of a given number
def findfactors(n):
    # for i in range(1,int(sqrt(n)+1)):
    for i in range(1,n+1):
        if n/i == int(n/i):
            factors.append(i)

# Function that checks if a number is prime
def checkprime(n):
    # Trial division
    for i in range(2,int(sqrt(n)+1)):
        if n/i == int(n/i):
            # Number gives a remainder upon division and therefore is not prime
            isprime = False
            break
        else:
            isprime = True
    if isprime == True:
        return True
    elif isprime == False:
        return False

I’m sure that to experts that code looks horrible. But it works if I use the Python shell:

>>> checkprime(9)
False
>>> checkprime(79)
True
>>> checkprime(factors[3])
True

But when I run the program with F5 I get:

Traceback (most recent call last):
  File "/home/alby/euler-python/euler3.py", line 45, in <module>
    checkprime(factors[i])
  File "/home/alby/euler-python/euler3.py", line 32, in checkprime
    if isprime == True:
UnboundLocalError: local variable 'isprime' referenced before assignment

If I call the checkprime function from within the program with a hardcoded number (e.g. checkprime(77)) I get no output at all. I’m certain that this is something basic about the way that Python works that I don’t understand, but I cannot for the life of me work out what.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:42:10+00:00Added an answer on June 7, 2026 at 12:42 pm

    In your Github code, we can see that you’re trying to call checkprime(1) (on the first iteration through your last loop).

    # Check each factor to see if it is prime or compound
    for i in range(0,len(factors)):
        print (factors[i])
        # Why can't I call checkprime here, like this? It works in the console.
        checkprime(factors[i])
    

    But look at your code:

    def checkprime(n):
        # Trial division
        for i in range(2,int(sqrt(n)+1)):
            if n/i == int(n/i):
                # Number gives a remainder upon division and therefore is not prime
                isprime = False
                break
            else:
                isprime = True
    

    If n = 1, then range(2, int(sqrt(1)+1)) is range(2,2) which is empty… so isprime never gets set, because the loop body never gets run.

    Keep in mind that the arguments to range() are a half-open interval – range(x,y) is “integers starting at x and ending before y”. Thus range(2,3) = [2] and range(2,2) = [].

    Another issue here is that findfactors() is returning 1 as the first factor – this is probably not what you want:

    def findfactors(n):
        # for i in range(1,int(sqrt(n)+1)):
        for i in range(1,n+1):
    

    For prime factorization checking, you probably want to start at 2, not 1 (since everything is divisible by 1).


    Also, this code is redundant:

    if isprime == True:
            return True
        elif isprime == False:
            return False
    

    You can really just write this as…

    return isprime
    

    Or you can go one step better and never use isprime in the first place – just replace isprime = True with return True and isprime = False with return False.

    Finally, a shorthand for int(n/i) is n // i – Python’s // operator does integer division.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.