Basic Python: Function giving me a global name error

anon_swe

I'm writing a function that will determine if a given number if "ugly". It's ugly if the only prime factors it has are 2, 3, and 5 (it can have less than all 3 but no others).

I get an error, saying: "Line 22: NameError: global name 'getPrimeFactors' is not defined".

What am I doing incorrectly?

import math

class Solution(object):
    def isPrime(self, num):
        for i in range(2, math.floor(math.sqrt(num))):
            if num % i == 0:
                return false
        return true

    def getPrimeFactors(self, num):
        primeFactors = []
        for i in range(2, math.floor(math.sqrt(num))):
            if isPrime(i) and num % i == 0:
                primeFactors.append(i)
        return primeFactors

    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        primeFactors = getPrimeFactors(num)
        for factor in primeFactors:
            if factor != 2 and factor != 3 and factor != 5:
                return false
        return true

Thanks!

bigOther

Use getPrimeFactors as self.getPrimeFactors(num) because it's defined as a method inside the class Solution:

primeFactors = self.getPrimeFactors(num)

The same for isPrime:

if self.isPrime(i) and num % i == 0:

Other issues:

  • range arguments should be int not float, so use int instead of math.floor:

    range(2, int(math.sqrt(num)))
    
  • Use True not true in python

    return True
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my if statement in Python giving me an error?

From Dev

Global name error in python program

From Dev

Variable name same as function name giving compiler error... Why?

From Dev

strchr function keep giving me core dumped error

From Dev

Python Error: NameError: global name 'ftp' is not defined

From Dev

opencv-python basic ORB feature detection gives me error -215 in function cv::drawKeypoints

From Dev

opencv-python basic ORB feature detection gives me error -215 in function cv::drawKeypoints

From Dev

Formatting a string in Python is giving me a file not found error

From Dev

Function in javascript giving error

From Dev

Python Shell: Global name 't' is not define. Me: but it is

From Dev

Django is giving me a 404 error

From Dev

Spinner giving me a NullPointerException error

From Dev

JSON Validator giving me error

From Dev

PyInstaller giving me a syntax error

From Dev

.done ajax giving me an error

From Dev

PyInstaller giving me a syntax error

From Dev

.done ajax giving me an error

From Dev

SMS manager giving me this error

From Dev

Name error in python with nested function

From Dev

can a python function call a global function with the same name?

From Dev

Uncaught Error: Mismatched anonymous define() module: function definition(name, global)

From Dev

Uncaught Error: Mismatched anonymous define() module: function definition(name, global)

From Dev

Windows 7 Task Scheduler is giving me an error saying "The specified account name is not valid"?

From Dev

Python recursive function giving different error or correct output

From Dev

Django 'global name' error

From Dev

"Global name is not defined" error

From Dev

Python range( ) is not giving me a list

From Dev

C Global Variable Code giving an compilation error

From Dev

Python giving a memory error

Related Related

  1. 1

    Why is my if statement in Python giving me an error?

  2. 2

    Global name error in python program

  3. 3

    Variable name same as function name giving compiler error... Why?

  4. 4

    strchr function keep giving me core dumped error

  5. 5

    Python Error: NameError: global name 'ftp' is not defined

  6. 6

    opencv-python basic ORB feature detection gives me error -215 in function cv::drawKeypoints

  7. 7

    opencv-python basic ORB feature detection gives me error -215 in function cv::drawKeypoints

  8. 8

    Formatting a string in Python is giving me a file not found error

  9. 9

    Function in javascript giving error

  10. 10

    Python Shell: Global name 't' is not define. Me: but it is

  11. 11

    Django is giving me a 404 error

  12. 12

    Spinner giving me a NullPointerException error

  13. 13

    JSON Validator giving me error

  14. 14

    PyInstaller giving me a syntax error

  15. 15

    .done ajax giving me an error

  16. 16

    PyInstaller giving me a syntax error

  17. 17

    .done ajax giving me an error

  18. 18

    SMS manager giving me this error

  19. 19

    Name error in python with nested function

  20. 20

    can a python function call a global function with the same name?

  21. 21

    Uncaught Error: Mismatched anonymous define() module: function definition(name, global)

  22. 22

    Uncaught Error: Mismatched anonymous define() module: function definition(name, global)

  23. 23

    Windows 7 Task Scheduler is giving me an error saying "The specified account name is not valid"?

  24. 24

    Python recursive function giving different error or correct output

  25. 25

    Django 'global name' error

  26. 26

    "Global name is not defined" error

  27. 27

    Python range( ) is not giving me a list

  28. 28

    C Global Variable Code giving an compilation error

  29. 29

    Python giving a memory error

HotTag

Archive