Negative print, it only prints the postives

Asked

Viewed 103 times

2

Write a program that reads whole numbers from the standard input until a negative number is reported. At each reading the number read should be written in the standard output.

n1 = int(input())
while n1:
  if n1 >= 0:
    print (n1)
    n1 = int(input())
  else:
    print (n1)
break

He has to print the negative number and it doesn’t happen.

  • 1

    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

2 answers

3

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.

  • Correct interpretation of the exercise, but I tried to print a negative number using the print, in python, and I couldn’t, is some limitation of the print? Where can I find more information on the subject?

  • 1

    @Luizaugusto no problem: https://ideone.com/9g3Wbd

  • Sure! I had put inside the loop if n1 < 0: , my fault, thank you!

0

From what I understand of your statement, you need to create a program that leia a number inteiro and display it. E, in case the number is negativo, also shows it, porém, Close the program right away.

Given this understanding, I developed the following algorithm...

from time import sleep

while True:
    try:
        n = int(input('Digite um número inteiro: '))
        if n >= 0:
            print(f'\033[32mO valor digitado foi: {n}\033[m')
        else:
            print(f'\033[32mO valor digitado foi: {n}\033[m')
            print('\033[31mEncerrando o programa!')
            for c in range(24):
                sleep(0.1)
                print(f'{chr(46)}', end='')
            print()
            break
    except:
        print('\033[31mValor INVÁLIDO! Digite apenas números inteiros!\033[m')

Note the functioning of the algorithm in repl it..

Also note that this algorithm has an error handling and exceptions, restricting only acceptance of numbers inteiros.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.