TypeError: 'NoneType' object is not iterable; Not iterating

This is the place for queries that don't fit in any of the other categories.

TypeError: 'NoneType' object is not iterable; Not iterating

Postby mckryall » Sat Feb 08, 2014 7:37 am

I finally decided to rewrite my slope-intercept form equation calculator for fraction support. I'm getting this error while passing variables to functions (None of them are lists, nor do I have any loops in my code):
Code: Select all
Traceback (most recent call last):
  File "s_i_form.py", line 202, in <module>
    slope_numer, slope_denom = slope_find(xnum1, xnum2, xden1, xden2, ynum1, ynum2, yden1, yden2)
  File "s_i_form.py", line 131, in slope_find
    alt_ynum1, alt_ynum2, alt_yden2_1 = denom_equalize(yden2, yden1, ynum1, ynum2)
TypeError: 'NoneType' object is not iterable

I could not reproduce the problem in a short enough manner, so would anyone mind clicking on this link to help me? (Kind of like google docs, a multiperson live editor with chat) http://collabedit.com/trahm
Any help is appreciated.
Last edited by Mekire on Sat Feb 08, 2014 8:01 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: TypeError: 'NoneType' object is not iterable; Not iterat

Postby Mekire » Sat Feb 08, 2014 8:28 am

Adding code pre-edit to post. If you have solved the problem, please post your solution:
Code: Select all
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
#------------------------------------------------------------------------------------------------------------------------
def lcm(a, b):
    return a * b // gcd(a, b)
#------------------------------------------------------------------------------------------------------------------------
def denom_equalize(den1, den2, num1, num2):
    if den2 != den1:
        alt_den2_1 = den2 * den1
        alt_num1 = num1 * den2
        alt_num2 = num2 * den1
        return  alt_num1, alt_num2, alt_den2_1
#------------------------------------------------------------------------------------------------------------------------
def frac_test(input):
    input = list(input)
    length = len(input) - 1
    if "/" in input:
        ch = input.index("/")
        if ch == 1:
            if length == 2:
                numer = int(input[0])
                denom = int(input[2])
            elif length == 3:
                numer = int(input[0])
                denom = int(str(input[2]) + str(input[3]))
            elif length == 4:
                numer = int(input[0])
                denom = int(str(input[2]) + str(input[3]) + str(input[4]))
        elif ch == 2:
            if length == 3:
                numer = int(str(input[0]) + str(input[1]))
                denom = int(input[3])
            elif length == 4:
                numer = int(str(input[0]) + str(input[1]))
                denom = int(str(input[3]) + str(input[4]))
            elif length == 5:
                numer = int(str(input[0]) + str(input[1]))
                denom = int(str(input[3]) + str(input[4]) + str(input[5]))
        elif ch == 3:
            if length == 4:
                numer = int(str(input[0]) + str(input[1]) + str(input[2]))
                denom = int(input[4])
            elif length == 5:
                numer = int(str(input[0]) + str(input[1]) + str(input[2]))
                denom = int(str(input[4]) + str(input[5]))
            elif length == 6:
                numer = int(str(input[0]) + str(input[1]) + str(input[2]))
                denom = int(str(input[4]) + str(input[5]) + str(input[6]))
    elif "/" not in input and length == 0:
        numer = int(input[0])
        denom = 1
    elif "/" not in input and length == 1:
        numer = int(str(input[0]) + str(input[1]))
        denom = 1
    elif "/" not in input and length == 2:
        numer = int(str(input[0]) + str(input[1]) + str(input[2]))
        denom = 1
    return numer, denom
#------------------------------------------------------------------------------------------------------------------------
def display1(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2):
    #for display purposes, not useful
    if xden1 != 1:
        x1 = "%d/%d" % (xnum1, xden1)
    else:
        x1 = "%d" % xnum1
    if xden2 != 1:
        x2 = "%d/%d" % (xnum2, xden2)
    else:
        x2 = "%d" % xnum2
    if yden1 != 1:
        y1 = "%d/%d" % (ynum1, yden1)
    else:
        y1 = "%d" % ynum1
    if yden2 != 1:
        y2 = "%d/%d" % (ynum2, yden2)
    else:
        y2 = "%d" % ynum2
    #-----
    print "Finding slope:"
    print y2, "-", y1
    print "----------"
    print x2, "-", x1
#------------------------------------------------------------------------------------------------------------------------
def slope_find(xnum1, xnum2, xden1, xden2, ynum1, ynum2, yden1, yden2):
    #display1(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2)
    #---
    if yden1 != yden2:
        lcm2 = lcm(yden1, yden2)
        if yden1 == lcm2 and yden2 == 1:
            ynum2 = ynum2 * lcm2
            yden2 = lcm2
        elif yden2 == lcm2 and yden1 == 1:
            ynum1 = ynum1 * lcm2
            yden1 = lcm2
        elif yden1 == yden2:
            pass
        elif yden1 != 1 and yden2 != 1:
            mlt_lcm3 = lcm2 / yden1
            mlt_lcm4 = lcm2 / yden2
            ynum1 = ynum1 * mlt_lcm3
            ynum2 = ynum2 * mlt_lcm4
            yden1 = lcm2
            yden2 = lcm2

    if xden1 != xden2:
        lcm2 = lcm(xden1, xden2)
        if xden1 == lcm2 and xden2 == 1:
            xnum2 = xnum2 * lcm2
            xden2 = lcm2
        elif xden2 == lcm2 and xden1 == 1:
            xnum1 = xnum1 * lcm2
            xden1 = lcm2
        elif xden1 == xden2:
            pass
        elif xden1 != 1 and xden2 != 1:
            mlt_lcm3 = lcm2 / xden1
            mlt_lcm4 = lcm2 / xden2
            xnum1 = xnum1 * mlt_lcm3
            xnum2 = xnum2 * mlt_lcm4
            xden1 = lcm2
            xden2 = lcm2
            #display(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2)
    print """
%d/%d - %d/%d
----------
%d/%d - %d/%d
""" % (ynum2, yden2, ynum1, yden1, xnum2, xden2, xnum1, xden1)
    alt_ynum1, alt_ynum2, alt_yden2_1 = denom_equalize(yden2, yden1, ynum1, ynum2)
    alt_xnum1, alt_xnum2, alt_xden2_1 = denom_equalize(xden2, xden1, xnum1, xnum2)
    time.sleep(1)
    print """
%d/%d - %d/%d
----------
%d/%d - %d/%d
""" % (alt_ynum2, alt_yden2_1, alt_ynum1, alt_yden2_1, alt_xnum2, alt_xden2_1, alt_xnum1, alt_xden2_1)
    slope_numer = alt_ynum2 - alt_ynum1
    slope_denom = alt_xnum2 - alt_ynum1
    print """
%d
---
%d
""" % (slope_numer, slope_denom)
    return slope_numer, slope_denom
#------------------------------------------------------------------------------------------------------------------------
def y_intercept_find(slope_numer, slope_denom, xnum1, xden1, ynum1, yden1):
    print """
%d    %d  %d
--- = --- --- + b
%d    %d  %d
""" % (ynum1, slope_numer, xnum1, yden1, slope_denom, xden1)
    m_x_numer = slope_numer * ynum1
    m_x_denom = slope_denom * yden1
    print """
%d    %d
--- = --- + b
%d    %d
"""
    alt_num1, alt_num2, alt_den2_1 = denom_equalize(yden1, m_x_denom, ynum1, m_x_numer)
    print """
%d    %d
--- - --- = b
%d    %d
""" % (alt_num1, alt_num2, alt_den2_1, alt_den2_1)
    y_inter_num = alt_num1 - alt_num2
    y_inter_den = alt_den2_1
    return y_inter_num, y_inter_den
#------------------------------------------------------------------------------------------------------------------------
try:
    twopq = raw_input("Do you have two ordered pairs?\n> ")
    if twopq.lower() in ['n', 'no']:
        print "Point-slope form not yet supported."
        exit()

    elif twopq.lower() in ['y', 'yes']:
        print "Good. Please input them now."
        x1 = raw_input("\nx 1:\n> ")
    xnum1, xden1 = frac_test(x1)
    y1 = raw_input("\ny 1:\n> ")
    ynum1, yden1 = frac_test(y1)
    print """\nOrdered pair 1:
%d   %d
--, --
%d   %d\n""" % (xnum1, ynum1, xden1, yden1)
    #---
    x2 = raw_input("\nx 2:\n> ")
    xnum2, xden2 = frac_test(x2)
    y2 = raw_input("\ny 2:\n> ")
    ynum2, yden2 = frac_test(y2)
    print """\nOrdered pair 2:
%d   %d
--, --
%d   %d""" % (xnum2, ynum2, xden2, yden2)
    #---

#-----
    slope_q = raw_input("Do you have the slope?\n> ")
    if slope_q.lower() in ['n', 'no']:
        print "Ok."
        slope_numer, slope_denom = slope_find(xnum1, xnum2, xden1, xden2, ynum1, ynum2, yden1, yden2)

    elif slope_q.lower() in ['y', 'yes']:
        print "Good. Please input the numerator and denominator values now."
        slope_numer = int(raw_input("Numerator:\n> "))
        slope_denom = int(raw_input("Denominator:\n> "))

    y_inter_q = raw_input("Do you have the y-intercept?/n ")
    if y_inter_q.lower() in ['n', 'no']:
        y_inter_num, y_inter_denom = y_intercept_find(slope_numer, slope_denom, xnum1, xden1, ynum1, yden1)
    elif y_inter_q.lower in ['y', 'yes']:
        print "Good. Please input the numerator and denominator values now."
        y_inter_num = int(raw_input("Numerator:\n> "))
        y_inter_denom = int(raw_input("Denominator:\n> "))
except KeyboardInterrupt:
    exit()

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: TypeError: 'NoneType' object is not iterable; Not iterat

Postby stranac » Sat Feb 08, 2014 10:44 am

Code: Select all
alt_ynum1, alt_ynum2, alt_yden2_1 = denom_equalize(yden2, yden1, ynum1, ynum2)

You're trying to do tuple unpacking here. That will require an iterable of 3 elements.
Code: Select all
>>> a, b, c = (1, 2, 3)
>>> a, b, c = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable


Seems like the denom_equalize() function is returning None.
That would happen when den1 and den2 are equal.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: TypeError: 'NoneType' object is not iterable; Not iterat

Postby mckryall » Sat Feb 08, 2014 4:52 pm

It turns out that code didn't even need to be there. I did need it somewhere else, though. I fixed the "None" problem with an else statement.
Code: Select all
import time
def gcd(a, b):
    while b:     
        a, b = b, a % b
    return a
#------------------------------------------------------------------------------------------------------------------------
def lcm(a, b):
    return a * b #// gcd(a, b) #Do not use because somethings don't work with this
#------------------------------------------------------------------------------------------------------------------------
def denom_equalize(den1, den2, num1, num2):
   if den2 != den1:
      alt_den2_1 = den2 * den1
      alt_num1 = num1 * den2
      alt_num2 = num2 * den1
      return  alt_num1, alt_num2, alt_den2_1
   else:
      return num1, num2, den1
#------------------------------------------------------------------------------------------------------------------------
def frac_test(input):
   input = list(input)
   length = len(input) - 1
   if "/" in input:
      ch = input.index("/")
      if ch == 1:
         if length == 2:
            numer = int(input[0])
            denom = int(input[2])
         elif length == 3:
            numer = int(input[0])
            denom = int(str(input[2]) + str(input[3]))
         elif length == 4:
            numer = int(input[0])
            denom = int(str(input[2]) + str(input[3]) + str(input[4]))
      elif ch == 2:
         if length == 3:
            numer = int(str(input[0]) + str(input[1]))
            denom = int(input[3])
         elif length == 4:
            numer = int(str(input[0]) + str(input[1]))
            denom = int(str(input[3]) + str(input[4]))
         elif length == 5:
            numer = int(str(input[0]) + str(input[1]))
            denom = int(str(input[3]) + str(input[4]) + str(input[5]))
      elif ch == 3:
         if length == 4:
            numer = int(str(input[0]) + str(input[1]) + str(input[2]))
            denom = int(input[4])
         elif length == 5:
            numer = int(str(input[0]) + str(input[1]) + str(input[2]))
            denom = int(str(input[4]) + str(input[5]))
         elif length == 6:
            numer = int(str(input[0]) + str(input[1]) + str(input[2]))
            denom = int(str(input[4]) + str(input[5]) + str(input[6]))
   elif "/" not in input and length == 0:
      numer = int(input[0])
      denom = 1
   elif "/" not in input and length == 1:
      numer = int(str(input[0]) + str(input[1]))
      denom = 1
   elif "/" not in input and length == 2:
      numer = int(str(input[0]) + str(input[1]) + str(input[2]))
      denom = 1
   return numer, denom
#------------------------------------------------------------------------------------------------------------------------
def display1(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2):
   #for display purposes, not useful
   if xden1 != 1:
      x1 = "%d/%d" % (xnum1, xden1)
   else:
      x1 = "%d" % xnum1
   if xden2 != 1:
      x2 = "%d/%d" % (xnum2, xden2)
   else:
      x2 = "%d" % xnum2
   if yden1 != 1:
      y1 = "%d/%d" % (ynum1, yden1)
   else:
      y1 = "%d" % ynum1
   if yden2 != 1:
      y2 = "%d/%d" % (ynum2, yden2)
   else:
      y2 = "%d" % ynum2
   #-----
   print "Finding slope:"
   print y2, "-", y1
   print "----------"
   print x2, "-", x1
#------------------------------------------------------------------------------------------------------------------------
def slope_find(xnum1, xnum2, xden1, xden2, ynum1, ynum2, yden1, yden2):
   #display1(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2)
   #---
   lcm1 = lcm(yden1, yden2)
   if yden1 == lcm1 and yden2 == 1:
      ynum2 = ynum2 * lcm1
      yden2 = lcm1
   elif yden2 == lcm1 and yden1 == 1:
      ynum1 = ynum1 * lcm1
      yden1 = lcm1
   elif yden1 == yden2:
      pass
   elif yden1 != 1 and yden2 != 1:
      mlt_lcm3 = yden2 #lcm1 / yden1
      mlt_lcm4 = yden1 #lcm1 / yden2
      ynum1 = ynum1 * mlt_lcm3
      ynum2 = ynum2 * mlt_lcm4
      yden1 = lcm1
      yden2 = lcm1

   lcm2 = lcm(xden1, xden2)
   if xden1 == lcm2 and xden2 == 1:
      xnum2 = xnum2 * lcm2
      xden2 = lcm2
   elif xden2 == lcm2 and xden1 == 1:
      xnum1 = xnum1 * lcm2
      xden1 = lcm2
   elif xden1 == xden2:
      pass
   elif xden1 != 1 and xden2 != 1:
      mlt_lcm5 = xden2 #lcm2 / xden1
      mlt_lcm6 = xden1 #lcm2 / xden2
      xnum1 = xnum1 * mlt_lcm5
      xnum2 = xnum2 * mlt_lcm6
      xden1 = lcm2
      xden2 = lcm2
      #display(xden1, xden2, xnum1, xnum2, yden1, yden2, ynum1, ynum2)
   print """
%d/%d - %d/%d
----------
%d/%d - %d/%d
""" % (ynum2, yden2, ynum1, yden1, xnum2, xden2, xnum1, xden1)
   alt_ynum1, alt_ynum2, alt_yden2_1 = denom_equalize(yden2, yden1, ynum1, ynum2)
   alt_xnum1, alt_xnum2, alt_xden2_1 = denom_equalize(xden2, xden1, xnum1, xnum2)
   slope_numer = alt_ynum2 - alt_ynum1
   slope_denom = alt_xnum2 - alt_xnum1
   time.sleep(1)
   print """
%d
---
%d
""" % (slope_numer, slope_denom)
   return slope_numer, slope_denom
#------------------------------------------------------------------------------------------------------------------------
def y_intercept_find(slope_numer, slope_denom, xnum1, xden1, ynum1, yden1):
   print """
%d      %d  %d
--- = --- --- + b
%d      %d  %d
""" % (ynum1, slope_numer, xnum1, yden1, slope_denom, xden1)
   m_x_numer = slope_numer * xnum1
   m_x_denom = slope_denom * xden1
   print """
%d      %d
--- = --- + b
%d      %d
""" % (ynum1, m_x_numer, yden1, m_x_denom)
   alt_num1, alt_num2, alt_den2_1 = denom_equalize(yden1, m_x_denom, ynum1, m_x_numer)
   print """
%d    %d
--- - --- = b
%d    %d
""" % (alt_num1, alt_num2, alt_den2_1, alt_den2_1)
   y_inter_num = alt_num1 - alt_num2
   y_inter_den = alt_den2_1
   print """
%d
--- = b
%d
""" % (y_inter_num, y_inter_denom)
   return y_inter_num, y_inter_den
#------------------------------------------------------------------------------------------------------------------------
try:
   twopq = raw_input("Do you have two ordered pairs?\n> ")
   if twopq.lower() in ['n', 'no']:
      print "Point-slope form not yet supported."
      exit()
      
   elif twopq.lower() in ['y', 'yes']:
      print "Good. Please input them now."
      x1 = raw_input("\nx 1:\n> ")
   xnum1, xden1 = frac_test(x1)
   y1 = raw_input("\ny 1:\n> ")
   ynum1, yden1 = frac_test(y1)
   print """\nOrdered pair 1:
%d   %d
--, --
%d   %d\n""" % (xnum1, ynum1, xden1, yden1)
   #---
   x2 = raw_input("\nx 2:\n> ")
   xnum2, xden2 = frac_test(x2)
   y2 = raw_input("\ny 2:\n> ")
   ynum2, yden2 = frac_test(y2)
   print """\nOrdered pair 2:
%d   %d
--, --
%d   %d""" % (xnum2, ynum2, xden2, yden2)
   #---
      
#-----
   slope_q = raw_input("Do you have the slope?\n> ")
   if slope_q.lower() in ['n', 'no']:
      print "Ok."
      slope_numer, slope_denom = slope_find(xnum1, xnum2, xden1, xden2, ynum1, ynum2, yden1, yden2)
      
   elif slope_q.lower() in ['y', 'yes']:
      print "Good. Please input the numerator and denominator values now."
      slope_numer = int(raw_input("Numerator:\n> "))
      slope_denom = int(raw_input("Denominator:\n> "))
      
   y_inter_q = raw_input("Do you have the y-intercept?\n> ")
   if y_inter_q.lower() in ['n', 'no']:
      y_inter_num, y_inter_denom = y_intercept_find(slope_numer, slope_denom, xnum1, xden1, ynum1, yden1)
   elif y_inter_q.lower in ['y', 'yes']:
      print "Good. Please input the numerator and denominator values now."
      y_inter_num = int(raw_input("Numerator:\n> "))
      y_inter_denom = int(raw_input("Denominator:\n> "))
except KeyboardInterrupt:
   exit()
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: 7stud and 6 guests