lundi 8 octobre 2018

How can I make my multiple if statements work? Or preferably how do I use nested loops in my code

What I want to do, is transform my variable:

table = "female\tIngelin\tAleksandersen\nmale\tJohnny\tDigre\nmale\tOsman\tBremseth\nfemale\tCathrine\tDagestad\nfemale\tTirill\tBakker"

Into a kind of scheme like this:

  1. Row: 0 Col: 0 Cell value: female
  2. Row: 0 Col: 1 Cell value: Ingelin
  3. Row: 0 Col: 2 Cell value: Aleksandersen
  4. Row: 1 Col: 0 Cell value: male
  5. Row: 1 Col: 1 Cell value: Johnny

The list is long, so ill stop at 5, but you get the point. My code so far:

table="female\tIngelin\tAleksandersen\nmale\tJohnny\tDigre\nmale\tOsman\tBremseth\nfemale\tCathrine\tDagestad\nfemale\tTirill\tBakker"

def show_table():
    print(table)

def show_every_cell():
    col = -1
    row = -1
names = table.split("\t")
for x in names:
    row += 1
    if row == 0:
        col += 1
        if col == 0:
            print('Row: 0 Col: 0 ' + 'Cell value: ' + x)
        if col == 1:
            print('Row: 0 Col: 1 ' + 'Cell value: ' + x)
        if col == 2:
            print('Row: 0 Col: 2 ' + 'Cell value: ' + x)

    if row == 1:
        col += 1
        if col == 0:
            print('Row: 1 Col: 0 ' + 'Cell value: ' + x)
        if col == 1:
            print('Row: 1 Col: 1 ' + 'Cell value: ' + x)
        if col == 2:
            print('Row: 1 Col: 2 ' + 'Cell value: ' + x)

    #The list continues with row: 2, 3 and 4. But no point in showing that part. 

def main():
    show_table()
    show_every_cell()

if __name__ == "__main__":
    main()

My output is this:

Row: 0 Col: 0 Cell value: female
Row: 1 Col: 1 Cell value: Ingelin

As you see, it misses quite a lot....

Aucun commentaire:

Enregistrer un commentaire