technocrat
3/5/2018 - 12:35 AM

iPython session transcript in re https://courses.edx.org/courses/course-v1:UCSanDiegoX+CSE100x+1T2018/discussion/forum/course/threads/5a9a12

 $ 502:  ip
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: # Comments by Richard Careaga (Nostromo) on niemasd guarantee 2018-03-04
   ...: def print_info(a):
   ...:     n = len(a)
   ...:     avg = 0.0
   ...:     for i in range(n):
   ...:         print("Element #%d is %d" % (i,a[i]))
   ...:         avg += a[i]
   ...:     avg /= n
   ...:     print("Average is %f" % avg)
   ...:

In [2]: print_info(15)
   ...:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-088e45cb4e72> in <module>()
----> 1 print_info(15)

<ipython-input-1-02fda1e35bcf> in print_info(a)
      1 # Comments by Richard Careaga (Nostromo) on niemasd guarantee 2018-03-04
      2 def print_info(a):
----> 3     n = len(a)
      4     avg = 0.0
      5     for i in range(n):

TypeError: object of type 'int' has no len()

In [3]: arg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
   ...:

In [4]: print_info(arg)
   ...:
Element #0 is 1
Element #1 is 2
Element #2 is 3
Element #3 is 4
Element #4 is 5
Element #5 is 6
Element #6 is 7
Element #7 is 8
Element #8 is 9
Element #9 is 10
Element #10 is 11
Element #11 is 12
Element #12 is 13
Element #13 is 14
Element #14 is 15
Average is 8.000000

In [5]: # Too bad about lists being zero indexed
   ...:
   ...: # Without parsing the C++ code, it is not obvious that the argument to
   ...: # print_info is a list, rather than an int, so some error trapping is
   ...: # needed
   ...: import operator
   ...: def print_info2(a):
   ...:     if isinstance(a, list):
   ...:         n = len(a)
   ...:         avg = 0.0
   ...:         for i in range(n):
   ...:             print("Element #%d is %d" % (i,a[i]))
   ...:             avg += a[i]
   ...:         avg /= n
   ...:         return print("Average is %f" % avg)
   ...:     else:
   ...:         return print("Error: The argument to this function, 'a' must be a list.")
   ...:

In [6]: print_info2(15)
   ...:
Error: The argument to this function, 'a' must be a list.

In [7]: print_info2(arg)
   ...:
Element #0 is 1
Element #1 is 2
Element #2 is 3
Element #3 is 4
Element #4 is 5
Element #5 is 6
Element #6 is 7
Element #7 is 8
Element #8 is 9
Element #9 is 10
Element #10 is 11
Element #11 is 12
Element #12 is 13
Element #13 is 14
Element #14 is 15
Average is 8.000000

In [8]: # However, transliterating from C++ is entirely unPythonic, anyway
   ...: import operator
   ...: def print_info3(a):
   ...:     if isinstance(a, list):
   ...:         for element in a:
   ...:             print("Element #%d is %d" % (element, element))
   ...:         print("Average is %f" % (sum(a)/len(a)))
   ...:     else:
   ...:         return print("Error: The argument to this function, 'a' must be a list")
   ...:

In [9]: print_info3(15)
   ...:
Error: The argument to this function, 'a' must be a list

In [10]: print_info3(arg)
    ...:
Element #1 is 1
Element #2 is 2
Element #3 is 3
Element #4 is 4
Element #5 is 5
Element #6 is 6
Element #7 is 7
Element #8 is 8
Element #9 is 9
Element #10 is 10
Element #11 is 11
Element #12 is 12
Element #13 is 13
Element #14 is 14
Element #15 is 15
Average is 8.000000

In [11]: