Nov-01-2016, 06:39 PM
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??
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)