0

I am new to Python. I won't hide this is homework. I cannot figure out a way to iterate through multiple lists.

I originally wrote the below coding for one list ('lst') and it worked perfectly! Then I went back to add the other three lists and I don't know why I can't get it to work.

Any suggestions would be much appreciated. And if there are any tips on cleaning and refactoring, I'd appreciate it!.

 result='False'

 def magic(lst):
    count=0
    #Create a list.
    if count == 0:
        lst=[(4,9,2),(3,5,7),(8,1,6)]
        count==1
        result='True'
    elif count==1:
        lst=[(2,7,6),(9,5,1),(4,3,8)]
        count==2
        result='True'
    elif count==2:
        lst=[(1,2,3),(4,5,6),(7,8,9)]
        count==3
        result='True'
    else:
        lst=[(4,9,2),(3,5,5),(8,1,6)]
        result='True'
    return result 

    #set condition for magic square and print results
    if is_row(let) and is_col(let) and is_diag(lst):
        print('True')
    else:
        print("False")

#is_row function accepts a list as argument returns true or false       
def is_row(lst):
   if sum(lst[0]) == sum(lst[1]) == sum(lst[2]):
       result_R = True
   else:
       result_R = False
   return result_R

#is_col does the same thing as is_row

def is_col(lst):
    for i in lst:
        #select from the tuples in lst to make columns list
        col1=[i[0] for i in lst]

        col2=[i[1] for i in lst]

        col3=[i[2] for i in lst]

        if sum(col1) == sum(col2) == sum(col3):
            result_C = True
        else:
            result_C = False
        return result_C

def is_diag(lst):
    #unpack all tuples into single list for easier indexing.
    unpack1=[num for element in lst for num in element]
    #Diagonal one slice
    D1=unpack1[0:9:4]
    #reverse list...simpler way of preparing for Diagonal Two
    lst.reverse()
    #unpack the reversed list
    unpack2=[num for element in lst for num in element]
    #Diagonal 2 slice
    D2=unpack2[0:9:4]
    #Condition for sum comparison
    if sum(D1)==sum(D2):
        result_D = True
    else:
        result_D = False
    return result_D

magic(a,b,c,d)
  • It's really hard to tell what you're asking. What is the expected behavior of the code? What is the `count` variable supposed to do? What are you trying to do with the syntax `lst=a[(4,9,2),(3,5,7),(8,1,6)]`? Why do you have two return statements and then more code afterwards? Etc., etc. – augurar Feb 14 '16 at 02:49
  • I simply want to print result of True or False for each list as they run through the 3 supporting functions. If they adhere to each, it's true. I have since taken out the two return statements (and the "a, b,c,d")...my bad (like I said, I'm new..but I really enjoy it!). These lists would print: True True False False – Trey Howerton Feb 14 '16 at 02:55

0 Answers0