Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problems with the formatting

Write a program that requires users to enter integers, each in a separate line. The user indicates the end of the entry in a blank line. The program prints negated values. The program prints values ​​in the same line separated by a space.

n = input()
while n != "":
    n = int(n)
    print(-n, end=" ")
    n = input()
print(-n, end=" ")

This code works, but it needs help with the formatting.
The input should look like this:

  • 5
  • 0
  • -11

The output should look like this:
-5 0 11 -2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you want to input all values first and then print you need save them all to a list. Then, print them out after you’re doing inputting. You can do this by using join(), but note that you must convert from str to int and back to str with this.

all_nums = []
n = input()
while n != "":
    all_nums.append(str(-int(i)))
    n = input()
print(" ".join(all_nums))

Instead, if you would like to print the numbers out with a normal loop you can do this

all_nums = []
n = input()
while n != "":
    all_nums.append(-int(i))
    n = input()
for i in all_nums:
    print(i, end=" ")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading