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

  • Home
  • SEARCH
  • 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 8760217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:59:21+00:00 2026-06-13T14:59:21+00:00

I’m just starting to learn python and keep getting an error that I just

  • 0

I’m just starting to learn python and keep getting an error that I just can’t figure out. Any help would be massively appreciated. Basically, I keep getting the following error:

Enter an int: 8

Traceback (most recent call last):
  File "C:\Users\Samuel\Documents\Python Stuff\Find Prime Factors of Number.py", line 16, in <module>
    once_cycle()
  File "C:\Users\Samuel\Documents\Python Stuff\Find Prime Factors of Number.py", line 8, in once_cycle
    while x==0:
UnboundLocalError: local variable 'x' referenced before assignment

I see lots of people are having the same problem, but when I look at what people have told them to do I can’t figure it out. Anyway, my code is this. I’ve re-checked all my indentation and can’t see a problem with it. The aim of this program is to find the prime factors of an int (although it’s only 90% complete). It’s written in Python 2.7.3.

import math
testedInt = float(raw_input("Enter an int: "))
workingInt = testedInt
x = 0

def once_cycle():
    for dividor in range(1, int(math.floor(math.sqrt(testedInt))+1)):
        while x==0:
            print "Called"
            if (workingInt%dividor == 0):
                workingInt = workingInt/dividor
                x = 1
    if (workingInt > 1):
        once_cycle()
    return

once_cycle()

print workingInt

Thanks in advance for any help,

Sam

  • 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-13T14:59:22+00:00Added an answer on June 13, 2026 at 2:59 pm

    In your one_cycle() function you are at some point assigning to x:

            if (workingInt%dividor == 0):
                workingInt = workingInt/dividor
                x = 1
    

    This makes x a local variable. You are also referring to it with a test:

        while x==0:
    

    but before it is assigned to. This is the cause of your exception.

    Either add x = 0 at the start of your function, or declare it a global (if that is what you meant it to be). By the looks of it, you don’t use x outside of the function, so you probably didn’t mean it to be.

    The following works; workingInt is also being modified so it needs to be declared global:

    def once_cycle():
        global workingInt
        x = 0
    
        for dividor in range(1, int(math.floor(math.sqrt(testedInt))+1)):
            while x==0:
                print "Called"
                if (workingInt%dividor == 0):
                    workingInt = workingInt/dividor
                    x = 1
        if (workingInt > 1):
            once_cycle()
        return
    

    or, simplified:

    def once_cycle():
        global workingInt
    
        for dividor in range(1, int(math.sqrt(testedInt)) + 1):
            while True:
                if workingInt % dividor == 0:
                    workingInt = workingInt / dividor
                    break
        if workingInt > 1:
            once_cycle()
    

    int(floating_point_number) already takes the floor of a floating point argument.

    Note that you end up with an infinite loop if workingInt % dividor is not 0. The first time testedInt is an odd number, this is going to hit you, for example, and your loop will never exit.

    Take 11, for example; you’ll try the divisors 1, 2, and 3. While 1 is a divisor, workingInt will remain 11 and the loop breaks. Next for loop, the divisor is 2, and workingInt % 2 is not ever going to give you 0, so the loop will continue forever.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.