#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    From fractions import Fraction not working


    I am attempting to write a code where I pull in data from a file, formatted "3 15 0" "5 20 0" etc, where 0 denotes the end of a line and I want to convert the other numbers into denominators. I've stripped the line, gotten rid of the zeros, written a for loop that accesses each of the numbers...but I'm running into a problem with the fractions function, where it isn't importing the fraction module. I'm running 2.7, I'm not sure if that gives any insight. Thanks much.

    Code:
    from fractions import Fraction
    
    def main():
        inFile = open("data.in", "r")
        inFile = inFile.readlines()
        i = 0
        for line in inFile:
    
                line = line[:-1]
                line = line.split()
                print(line)
                length = len(line)
                for x in range(length-1):
                    var= (int(line[x]))
                    print (Fraction(1, var))
    
    main()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    What is the actual error you are getting? Does it actually give you the error:
    Code:
    ImportError: No module named fractions
    Does the following code work?
    Code:
    from fractions import Fraction
    print(Fraction(1, 5))
    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    The code does work, but when I try running my other code, the errors is "ImportError: cannot import name Fraction"

    Originally Posted by Mekire
    What is the actual error you are getting? Does it actually give you the error:
    Code:
    ImportError: No module named fractions
    Does the following code work?
    Code:
    from fractions import Fraction
    print(Fraction(1, 5))
    -Mek
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Did you by chance name one of your files in the same directory fractions?

    -Mek

    Comments on this post

    • b49P23TIvg agrees : Great sleuthing!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    Oh, that's definitely it! Thanks a ton


    Originally Posted by Mekire
    Did you by chance name one of your files in the same directory fractions?

    -Mek
    Last edited by HannahBrown; June 2nd, 2013 at 01:40 PM. Reason: Resolved
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Removed the extraneous code
    Code:
    from fractions import Fraction
    
    def main():
        inFile = open("data.in", "r")
        for line in inFile:
            for x in line.split()[:-1]:
                print(Fraction(1, int(x)))
    
    main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo