Python Forum
help with a script to determine length of an argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with a script to determine length of an argument
#1
Here is an assignment that i have:
Create a function called "length_of" that:
  1. Takes a single argument
  2. Return the length of argument, if its type supports it (e.g. list, tuple, string, dictionary, etc. have length)
  2. Prints out "Length is not supported" and returns None, if the passed argument does not support length (e.g. integer)
  3. When inspected for help returns the description:
    Safely get the length of the passed argument

Here are example arguments and expected results:
length_of('ab') -> 2
length_of([1,2,3]) -> 3
length_of({'foo':'bar'}) -> 1
length_of(0) -> None # print out "Length is not supported"
length_of(True) -> None # print out "Length is not supported"


Here is the script that i have come up with, but it does not work for numeric, float, tuple and list. what am i doing wrong here??

import sys
arg = sys.argv[1]
"""print arg"""
def length_of(arg):
   if type(arg) == type(int()) or type(arg) == type(float()) or type(arg) == type(bool()):
         print('Length is not supported')
   else:
         print len(arg)
length_of(arg)
Reply


Messages In This Thread
help with a script to determine length of an argument - by roadrage - Nov-01-2016, 06:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,880 Mar-03-2020, 08:34 AM
Last Post: buran
  How to determine pen color from an image? robie972003 2 2,438 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,831 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,273 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 4,063 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  How Do I Determine indentation in AST? jimo 3 4,305 Jul-01-2018, 04:25 PM
Last Post: buran
  Adding a parameter/argument to a script jehoshua 11 9,521 Jan-29-2018, 09:45 AM
Last Post: jehoshua
  Determine whether a method was overridden porton 6 6,193 Nov-14-2016, 09:51 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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