LotusAlice
LotusAlice

Reputation: 135

How to convert a for loop into a nested while loop w/ a working if statement in Python

I'm struggling at the moment in finding a way to convert my for loops to a good working set of while loops with a working if statement to create a multiplication table that looks like this:

Click here for Multiplication table image

Any suggestions or tips? I am using python 3.6.

This is what I have for the for loop to create the Multiplication table referenced above, how can I convert this to a working nested while loop:

print('x', end='\t')
for row in range (1,13):
    print(row, end='\t')
for row in range (1,13):
    print('')
    print()
    print()
    print(row, end='\t')
    for col in range(1,13):
        print(row*col, end='\t')

Upvotes: 0

Views: 626

Answers (1)

Pankaj Singh
Pankaj Singh

Reputation: 1178

I don't know your purpose but can do something like below

print('x', end='\t')
row = 1
while (row < 13):
    print(row, end='\t')
    row += 1
row = 1    
while (row < 13):
    print('')
    print()
    print()
    print(row, end='\t')
    col = 1
    while( col < 13):
        print(row*col, end='\t')
        col += 1
    row += 1

Upvotes: 1

Related Questions