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
#2
Are you meant to be using a try and except block ? because that would be a better way to do it.
Reply
#3
Don't know why you have this:
import sys
arg = sys.argv[1]
then use the same variable in your "def".  For now, comment it out or rename 'arg' something different, either on line 2, or with your 'length_of' function.

Next, your 'print' statement on line 8 is missing a pair of parenthesis.

A final note, keep your indentation uniform, the standard is 4 spaces.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(Nov-01-2016, 07:17 PM)Yoriz Wrote: Are you meant to be using a try and except block ? because that would be a better way to do it.

yes i can use a try and except block
Reply
#5
Hello!
The input argument always is a string. In order to check if it is an integer for example, you have to try to turn it to an int. Same is applied to others types.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
You still need to correct the other mistakes, even with a try/except.

The sys.argv is going to return either a one element list or a string. By using sys.argv[1] you will receive an error:
Error:
Traceback (most recent call last):   File "C:/Python/Samples/scratch.py", line 3, in <module>     file_loc = sys.argv[1] IndexError: list index out of range
Remember in Python, a list starts with '0', not a '1'

So:
file_loc = sys.argv[0]
print("file_loc = ", file_loc)
print("file_loc type = ", type(file_loc))
returns:
Output:
file_loc =  C:/Python/Samples/scratch.py file_loc type =  <class 'str'> Length of 'arg' is:  28
and:

file_loc = sys.argv
print("file_loc = ", file_loc)
print("file_loc type = ", type(file_loc))
returns:

Output:
"C:\Python 3.5\python.exe" C:/Python/Samples/scratch.py file_loc =  ['C:/Python/Samples/scratch.py'] file_loc type =  <class 'list'> Length of 'arg' is:  1
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Nov-01-2016, 08:20 PM)sparkz_alot Wrote: By using sys.argv[1] you will receive an error:
When using sys.argv[1] it's meant to be run from command line sparkz_alot.
From command line is sys.argv[0] script name(test_argv.py),
and sys.argv[1] argument(hello).
Output:
python test_argv.py hello 5
But i am not sure if it's meant to by run from command line.
It dos not say anything about that in task.
So is meant to by run bye command line roadrage?
It will also make it more difficult,because sys.argv return a string.
So passing in [1,2,3] will return string "[1,2,3]".
Reply
#8
It's not clear if the argument can be 1 2 3 also
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
yes it is meant to run from commandline....yes i confirm after testing the list, it is showing up as a string (I get a length of 1, instead of 3).
(Nov-01-2016, 09:59 PM)snippsat Wrote:
(Nov-01-2016, 08:20 PM)sparkz_alot Wrote: By using sys.argv[1] you will receive an error:
When using sys.argv[1] it's meant to be run from command line sparkz_alot.
From command line is sys.argv[0] script name(test_argv.py),
and sys.argv[1] argument(hello).
Output:
python test_argv.py hello 5
But i am not sure if it's meant to by run from command line.
It dos not say anything about that in task.
So is meant to by run bye command line roadrage?
It will also make it more difficult,because sys.argv return a string.
So passing in [1,2,3] will return string "[1,2,3]".
Reply
#10
Look at argparse,if you can use 3-party library Click.
Here a post about passing a list,from command line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,734 Mar-03-2020, 08:34 AM
Last Post: buran
  How to determine pen color from an image? robie972003 2 2,358 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,715 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,208 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 3,913 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  How Do I Determine indentation in AST? jimo 3 4,172 Jul-01-2018, 04:25 PM
Last Post: buran
  Adding a parameter/argument to a script jehoshua 11 9,302 Jan-29-2018, 09:45 AM
Last Post: jehoshua
  Determine whether a method was overridden porton 6 6,047 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