Python Forum
square root of 5 input numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: square root of 5 input numbers (/thread-27542.html)



square root of 5 input numbers - mrityunjoy - Jun-10-2020

Hi,

I have written the below piece of code, but output is showing list every time based on number of input. My expectation is to publish final list, how can I use return here
import math
def lists(*args):
    lst = []
    for i in args:
        ele = math.sqrt(int(i))
        lst.append("%.2f" % ele)
        print(lst)



RE: square root of 5 input numbers - perfringo - Jun-10-2020

(Jun-10-2020, 10:17 AM)mrityunjoy Wrote: how can I use return here

Use return outside of for-loop. This means that it returns after all elements are appended.

EDIT:

If you are happy returning list of strings (as your code does) then it's OK. If you need perform any calculations later, you should probably convert to float.

import math

def lists(*args):
    return [float(f'{math.sqrt(int(arg)):.2f}') for arg in args]