Is there a way in Python to do something to every single output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there a way in Python to do something to every single output?

I want to round off every number in the output of this program to 4 decimal places https://code.sololearn.com/cBMDxew781Hr/?ref=app Is there an easy way to do that without manually putting round() in every line?

19th Jul 2018, 6:19 AM
Satyam
8 Answers
+ 10
could you ellaborate well cuz from your code you used round() in only two lines of code where else did you want to apply it..!?
19th Jul 2018, 6:31 AM
Genes Sambayena
Genes Sambayena - avatar
+ 10
in your case you have to put it on every line then you have to put it in every line
19th Jul 2018, 6:36 AM
Genes Sambayena
Genes Sambayena - avatar
+ 10
yes you can using the if..else statement but in the case of returning only one or two outputs
19th Jul 2018, 6:46 AM
Genes Sambayena
Genes Sambayena - avatar
+ 4
good idea HonFu. Thank you!
19th Jul 2018, 11:57 AM
Satyam
+ 3
To every line which converts to another unit.
19th Jul 2018, 6:32 AM
Satyam
+ 3
Is there a way to do it in coee without doing it manually?
19th Jul 2018, 6:44 AM
Satyam
+ 3
how?
19th Jul 2018, 6:46 AM
Satyam
+ 3
How about writing a function? Your output seems to follow the same pattern: First there is some calculation, then there is a string addendum. def myformat (calculation, addendum): print(str(round(calculation, 4)) + addendum)
19th Jul 2018, 11:21 AM
HonFu
HonFu - avatar