1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Understanding the very basic "loop" code in python

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 19:03.

  1. Stack

    Stack Membro Participativo

    I stuck on one task that involved making a loop in order to print all values (squared) based on the User's first input. My code did a pretty good job in printing the values, HOWEVER... they were printed upside-down.

    Could someone help me understand the difference between these two approaches?

    Below you can see the actual "description of the exercise" and the code I have wrote for the mentioned exercise.

    Edit: Adding the Task description as requested.

    Task:

    The provided code stub reads and integer, n, from STDIN. For all non-negative integers i < n , print .

    Example:

    n = 3

    The list of non-negative integers that are less than n = 3 is [0,1,2]. Print the square of each number on a separate line.

    Input Format:

    The first and only line contains the integer, n.

    Constraints:

    1 ≤ n ≤ 20

    Output Format:

    Print lines, one corresponding to each i.

    Edit: Leaving the link to the screenshot, just in case. Hacker Rank exercise description

    I have tried the following part of the code:

    n = int(input())

    for i in range(n):
    print(n * n)
    n = n - 1


    But the one below worked out as expected:

    n = int(input())

    for n in range(n):
    print(n * n)
    n = n - 1

    Continue reading...

Compartilhe esta Página