[Python] Difficulty understanding nested loops with addition, range and print function

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    When I put this python code with input 12, the answer is 0, 6, 18. I dont know how to calculate that and I keep visualizing it as code snippet 2, with the answers 0,0,1,3,6,6,8,12.

    How does this loop work?

    stop=int(input())
    result=0
    for a in range(5):
    for b in range(4):
    result += a * b
    print(result)
    if result > stop:
    break


    What I calculate

    stop=int(input())
    result=0
    for a in range(5):
    for b in range(4):
    result += a * b
    print(result)
    if result > stop:
    break


    picture of my calculations

    Continue reading...

Compartilhe esta Página