Python Forum
square root of 5 input numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
square root of 5 input numbers
#1
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)
Reply
#2
(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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im at square one even with trying to install python origen 1 342 Jan-12-2024, 05:39 AM
Last Post: ndc85430
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,464 Jun-16-2022, 06:59 PM
Last Post: saoko
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,862 Sep-15-2021, 12:48 PM
Last Post: jenya56
  square root of 6 input numbers mrityunjoy 3 2,584 Jun-07-2020, 06:35 AM
Last Post: ndc85430
  How to simplify square finding program? meknowsnothing 3 2,883 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,707 May-09-2019, 12:19 PM
Last Post: Pleiades
  unable to pass a input after changing the user from root to non root using python avinash 3 3,165 Apr-08-2019, 10:05 AM
Last Post: avinash
  converting arguments or input numbers Skaperen 8 4,440 Aug-21-2018, 12:17 AM
Last Post: Skaperen
  cropping a picture (always square) Leon 1 2,124 Aug-13-2018, 10:04 AM
Last Post: Leon
  Error when trying to square a number pistacheoowalnuttanker 5 3,793 Jul-20-2018, 02:23 AM
Last Post: pistacheoowalnuttanker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020