2

I've been given out an assignment to write a program for a byteadder. The first program just checks if the entered number is valid or not and imports two other modules which are logicGates and fullAdder. logicGates has no errors, I am sure of it since it consists of gates ( if, else) and nothing more. Here's my code:

    dec1=int(input("Enter first decimal number: "))
    dec2=int(input("Enter second decimal number: "))

    def check(dec1,dec2):

        if (dec1+dec2)>255:
            print ("The decimal number you've entered is invalid. Please enter another number")
            dec1=int(input("Enter first number again: "))
            dec2=int(input("Enter second number again: "))
            return dec1,dec2

        elif (dec1+dec2)>+127 and (dec1+dec2)<-127:
            print ("The decimal number you've entered is invalid. Please enter your numbers again")
            dec1=int(input("Enter first number again: "))
            dec2=int(input("Enter second number again: "))
            return dec1,dec2

        else:
            print("Your number is now converted")
            return dec1,dec2

            print (check(dec1,dec2))

            import conversion
            lis1, lis2=conversion.bin2dec(dec1,dec2)

            import fullAdder
            add= fullAdder.fulladder(lis1,lis2)

            print (add)

And here's my fullAdder:

    import logicGates

    def fulladder(bin1,bin2):
        lis1=list(bin1)
        lis2=list(bin2)
        Cin=0
        lisEmp=[]

        for i in range(7,-1,-1):
            num1=int(lis1[i])
            num2=int(lis2[i])
            result1=logicGates.XOR(num1,num2)
            result2=logicGates.NAND(result1,Cin)
            result3=logicGates.OR(result1,Cin)
            Sum=logicGates.AND(result2,result3)
            result4=logicGates.AND(num1,num2)
            result5=logicGates.AND(result1,Cin)
            result6=logicGates.NOR(result4,result5)
            result7=logicGates.NOT(result6)

            Cin=result7
            lisEmp.insert(0,Sum)

        return lisEmp

Here's my code for bin2dec:

    def bin2dec(dec1,dec2):

        a=bin(dec1)
        b=bin(dec2)

        c=a[2:]
        d=b[2:]

        bin1=c.zfill(8)
        bin2=d.zfill(8)

        print (bin1)
        print (bin2)

This is the error I keep getting.

    Traceback (most recent call last):
      File "C:\Python34\main.py", line 26, in <module>
        lis1, lis2=conversion.bin2dec(dec1,dec2)
    TypeError: 'NoneType' object is not iterable

4 Answers4

2

TLDR

The problem is that your function bin2dec does not have a return statement. You can fix it by adding

return bin1, bin2

to the end of bin2dec.

In depth explanation of the error

What actually happens, and why you get "NoneType object is not iterable", is that python does in fact not have multiple return values from a function, even if it looks like it. Instead, what happens is that a tuple is returned, and that this tuple is then unpacked.

So, what happens in your code is equivalent to this

result = conversion.bin2dec(dec1, dec2)
lis1, lis2 = result

Here, since you lack a return statement, we get that result is None, and then in the next line when lis1, lis2 = result is executed you try to unpack a None value, which is not possible and hence throws a TypeError.

Jonas Adler
  • 10,365
  • 5
  • 46
  • 73
0

Your bin2dec function does not return values. It prints out values.

Try changing it to return bin1, bin2

Anirudh Sridhar
  • 183
  • 1
  • 12
0

I think that code like this is better:

def input2num():
    while 1:
        dec1 = int(input("Enter first decimal number: "))
        dec2 = int(input("Enter second decimal number: "))
        if not -127 <= dec1+dec2 <= 127:
            print ("The decimal numbers you've entered is invalid."
                   " Please enter another two numbers.\n")
        else:
            print("Your number is now converted")                

            import conversion
            lst1, lst2 = conversion.bin2dec(dec1, dec2)

            import fullAdder
            add = fullAdder.fulladder(lst1, lst2)
            print(add)
            return dec1, dec2

dec1, dec2 = input2num()            

fullAdder:

import logicGates

def fulladder(bin1, bin2):
    cin, lstEmp = 0, []
    for a, b in zip(bin1[::-1], bin2[::-1]): 
        num1, num2 = int(a), int(b)
        result1 = logicGates.XOR(num1, num2)
        result2 = logicGates.NAND(result1, cin)
        result3 = logicGates.OR(result1, cin)
        _sum = logicGates.AND(result2, result3)
        result4 = logicGates.AND(num1, num2)
        result5 = logicGates.AND(result1, cin)
        result6 = logicGates.NOR(result4, result5)
        result7 = logicGates.NOT(result6)
        cin = result7
        lstEmp.append(_sum)
    return lstEmp[::-1]

bin2dec:

def bin2dec(dec1, dec2):
    form = lambda x: bin(x)[2:].zfill(8)
    print(form(dec1), form(dec2))
    return form(dec1), form(dec2)
williezh
  • 917
  • 5
  • 8
0
def bin2dec(dec1,dec2):

        a=bin(dec1)
        b=bin(dec2)

        c=a[2:]
        d=b[2:]

        bin1=c.zfill(8)
        bin2=d.zfill(8)

        print (bin1)
        print (bin2)

There is no return statement in your function. Hence when it is called after execution it returns a None Value.

You can fix it by adding return(bin1,bin2) after the print

def bin2dec(dec1,dec2):

            a=bin(dec1)
            b=bin(dec2)

            c=a[2:]
            d=b[2:]

            bin1=c.zfill(8)
            bin2=d.zfill(8)

            print (bin1)
            print (bin2)
            return (bin1,bin2)