简体   繁体   English

Hackerrank 不接受我的代码。 为什么?

[英]Hackerrank doesn't accept my code. Why?

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 .所以任务是读取一个 integer N对于所有非负整数I < N ,打印 output 格式应该打印N行,一个对应于每个i

For example, the user input is 5 so the output should be... 0 1 4 9 16例如,用户输入是 5,所以 output 应该是... 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 3.7 中有效,但在 Hackerrank 中不起作用,因为如果您输入一个大于 5 的数字,比如 7,Hackerrank 将 output... 0 1 4 9 16 25 36

Python would've just stopped after outputting the number 16. Python 会在输出数字 16 后停止。

How can I fix this in Hackerrank?如何在 Hackerrank 中解决这个问题? Here is the link if you want to see the problem yourself.如果您想自己查看问题,请点击此处的链接。 https://www.hackerrank.com/challenges/python-loops/problem 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.首先,如果要遍历 n 个数字,则不应在 range(0,5) 中写入。

Secondly, You do not need to write another while function.其次,您不需要编写另一个 while 函数。 You use for loop or while loop for this question.对于这个问题,您可以使用for loopwhile loop

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:添加到@Marcus.Aurelianus 答案:

If you're in Python 2 change:如果您使用的是 Python 2 更改:

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.第一个是你不写你的 N 值,你必须让用户进入。

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.您需要将print(i ** 2)修复为print(i * 2)以便您可以获得他们想要的结果。 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)

In Python2:在 Python2 中:

if __name__ == '__main__':
    n = int(raw_input())
for number in range(0, n):
    print(number*number)

In Python3:在 Python3 中:

if __name__ == '__main__':
    n = int(input())
for number in range(0, n):
    print(number*number)

In python 3:在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)

This should work:这应该有效:

if __ name __ == ' __main __':
     n = int(input())

for i in range(n):
    while i < 10:
        print(i ** 2)
        i += 1
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM