Home > Net >  Having Trouble with the Output with Python Nested Loops
Having Trouble with the Output with Python Nested Loops

Time:01-18

I've been at this questions for over a day now, so looking for some help with what my next step should be. I believe I'm missing something key with my logic on loops or nested loops or variables or all of these topics. FYI I am a complete beginner.

Questions is as follows:

Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:

Please type in a number: 3

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 2
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
limit = int(input("Please type in a number: "))
i = 0
n = 2
something = 1
while i <= limit:
    i  = 1
    answer = i * something
    print(f"{i} x {something} = {answer}")
    while something < i :
        something  = 1
    while n <= limit:
        answer = i * n
        print(f"{i} x {n} = {answer}")
        n  = 1

I've tried debugging,, writing pseudocode, taking breaks, thinking...etc

I can get the first output correct

1 x 1
1 x 2
1 x i...

But after that my code is incorrect. I believe my problem is where I nest the loops, how many nested loops I need and how many variables I need.

Any help with how I should approach this problem would be appreciated.

I have tried variations of nesting the loops in different places and adding more variables.

CodePudding user response:

There are probably better ways to do this, but it should do the job:

limit = int(input("Please type in a number: "))
counter = 1
i = 1
for _ in range(limit):
    for _ in range(limit):
        calc = counter*i
        print(f"{counter} x {i} = {calc}")
        i  = 1
    counter  = 1
    i = 1

Result:

Please type in a number: 3
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9

Please type in a number: 6
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36

CodePudding user response:

I've reused your snippet but have simplified it a bit. Removed the unnecessary multiple while loops. As you can see, I've just got two counters. One to track each number till you reach the input limit, and the other is to track the incremental value. Once the value for i hits the limit, I'm resetting it to 0 so that I can increment for the next number (j):

limit = int(input("Please type in a number: "))
i = 1
n = 1  

while n <= limit:
    answer = i * n
    print(f"{n} x {i} = {answer}")
    if i == limit:
        n  = 1
        i = 0
    i  = 1

Output:

enter image description here

  • Related