How can I reverse the value of the output?
Question:
Let’s put int 5 as a sample input; in my code its expressed as this:
n = int(input())
for i in range(0, n):
n = n - 1
print(n**2)
I get an output as:
16
9
4
1
0
Instead I want to reverse the result to:
0
1
4
9
16
How do you go about solving this problem?
Answers:
The following will reverse the output:
n = int(input())
for i in range(0,n):
print(i**2)
It will loop from 0 to your inputted n
value, printing it’s square at each iteration. This also negates the need for the n = n - 1
line.
Output:
0 ** 2
1 ** 2
.
.
.
n ** 2
Let’s put int 5 as a sample input; in my code its expressed as this:
n = int(input())
for i in range(0, n):
n = n - 1
print(n**2)
I get an output as:
16
9
4
1
0
Instead I want to reverse the result to:
0
1
4
9
16
How do you go about solving this problem?
The following will reverse the output:
n = int(input())
for i in range(0,n):
print(i**2)
It will loop from 0 to your inputted n
value, printing it’s square at each iteration. This also negates the need for the n = n - 1
line.
Output:
0 ** 2
1 ** 2
.
.
.
n ** 2