Hackerrank doesn't accept my code. Why?
Question:
So the task is to read an integer N For all non-negative integers I < N, print
The output format should print N lines, one corresponding to each i.
For example, the user input is 5 so the output should be…
0
1
4
9
16
Here is my solution.
# The first two lines of code were default and already there.
if __name__ == '__main__':
n = int(input())
# Everything below is my code.
for i in range(0,5):
while i < 5:
print(i ** 2)
i += 1
break
So although this works in Python 3.7, it does not work in Hackerrank because if you were to input an number higher than 5, let’s say 7, Hackerrank would output…
0
1
4
9
16
25
36
Python would’ve just stopped after outputting the number 16.
How can I fix this in Hackerrank?
Here is the link if you want to see the problem yourself.
https://www.hackerrank.com/challenges/python-loops/problem
Answers:
Firstly, You should not write in range(0,5) if you want to iterate through n numbers.
Secondly, You do not need to write another while function. You use for loop
or while loop
for this question.
Change
for i in range(0,5):
while i < 5:
print(i ** 2)
i += 1
break
to
for i in range(0,n):
print(i ** 2)
To add to @Marcus.Aurelianus answer:
If you’re in Python 2 change:
n = input()
n=int(input())
for i in range(0,n):
print(i*i)
Because there are two wrong things in your answer.
The first one is that you don’t write your value for N, you’ve got to get the user in.
Secondly, the math operation is wrong. You need to fix print(i ** 2)
as print(i * 2)
so you can get the result they want. That’s probably why it doesn’t accept.
My answer to the same question had been as follows.
if __name__ == '__main__':
n = int(input(""))
i = 0
while i < n:
r = i * i
i += 1
print(r)
This should work:
if __ name __ == ' __main __':
n = int(input())
for i in range(n):
while i < 10:
print(i ** 2)
i += 1
break
In Python2:
if __name__ == '__main__':
n = int(raw_input())
for number in range(0, n):
print(number*number)
In Python3:
if __name__ == '__main__':
n = int(input())
for number in range(0, n):
print(number*number)
In python 3:
if __name__ == '__main__':
n = int(input())
for i in range(0,n):
if i<n:
print(i*i)
if __name__ == '__main__':
n = int(input())
for i in range(0,n):
(i*i) and 1 <=n <=20
print(i*i)
Here is a simple and easy-to-understand solution
if __name__ == '__main__':
n = int(input())
if n>-1: # it proceeds only in number in not negative int
for num in range(0,n): #takes one digit at a time from 0 to n
print(num**2)
So the task is to read an integer N For all non-negative integers I < N, print
The output format should print N lines, one corresponding to each i.
For example, the user input is 5 so the output should be…
0
1
4
9
16
Here is my solution.
# The first two lines of code were default and already there.
if __name__ == '__main__':
n = int(input())
# Everything below is my code.
for i in range(0,5):
while i < 5:
print(i ** 2)
i += 1
break
So although this works in Python 3.7, it does not work in Hackerrank because if you were to input an number higher than 5, let’s say 7, Hackerrank would output…
0
1
4
9
16
25
36
Python would’ve just stopped after outputting the number 16.
How can I fix this in Hackerrank?
Here is the link if you want to see the problem yourself.
https://www.hackerrank.com/challenges/python-loops/problem
Firstly, You should not write in range(0,5) if you want to iterate through n numbers.
Secondly, You do not need to write another while function. You use for loop
or while loop
for this question.
Change
for i in range(0,5):
while i < 5:
print(i ** 2)
i += 1
break
to
for i in range(0,n):
print(i ** 2)
To add to @Marcus.Aurelianus answer:
If you’re in Python 2 change:
n = input()
n=int(input())
for i in range(0,n):
print(i*i)
Because there are two wrong things in your answer.
The first one is that you don’t write your value for N, you’ve got to get the user in.
Secondly, the math operation is wrong. You need to fix print(i ** 2)
as print(i * 2)
so you can get the result they want. That’s probably why it doesn’t accept.
My answer to the same question had been as follows.
if __name__ == '__main__':
n = int(input(""))
i = 0
while i < n:
r = i * i
i += 1
print(r)
This should work:
if __ name __ == ' __main __':
n = int(input())
for i in range(n):
while i < 10:
print(i ** 2)
i += 1
break
In Python2:
if __name__ == '__main__':
n = int(raw_input())
for number in range(0, n):
print(number*number)
In Python3:
if __name__ == '__main__':
n = int(input())
for number in range(0, n):
print(number*number)
In python 3:
if __name__ == '__main__':
n = int(input())
for i in range(0,n):
if i<n:
print(i*i)
if __name__ == '__main__':
n = int(input())
for i in range(0,n):
(i*i) and 1 <=n <=20
print(i*i)
Here is a simple and easy-to-understand solution
if __name__ == '__main__':
n = int(input())
if n>-1: # it proceeds only in number in not negative int
for num in range(0,n): #takes one digit at a time from 0 to n
print(num**2)