Mainly the break
is in the wrong place, and without any condition, and if one will come out this command has no reason to complicate, and duplicate the condition. And the code can be much simpler without duplication.
It just needs to ask for the data inside the loop, check if it is negative, and if it is it should close, otherwise it continues the flow by printing and repeating. There is no reason to do otherwise than the statement asks, it is a matter of interpretation of text.
The question says you have to print the negative, but the statement is ambiguous about this, it is usually not the desired.
I did not check if the data was typed wrong, if something other than a number is typed the application will break.
while True:
n1 = int(input())
if n1 < 0:
break
print(n1)
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
If you really want the negative to be printed is just a matter of order that things should be executed, then if the if
establishes the output of the loop and wants it printed before leaving, just put the print()
before the if
.
while True:
n1 = int(input())
print(n1)
if n1 < 0:
break
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site
– Maniero