text iPython会议记录在https://courses.edx.org/courses/course-v1:UCSanDiegoX+CSE100x+1T2018/discussion/for

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text iPython会议记录在https://courses.edx.org/courses/course-v1:UCSanDiegoX+CSE100x+1T2018/discussion/for相关的知识,希望对你有一定的参考价值。

 $ 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]:

以上是关于text iPython会议记录在https://courses.edx.org/courses/course-v1:UCSanDiegoX+CSE100x+1T2018/discussion/for的主要内容,如果未能解决你的问题,请参考以下文章

python SublimeREPL ipy_repl.py用于在Sublime Text中运行IPython 4 / Jupyter

如何将 IPython 历史记录到文本文件?

删除ipython历史记录文件的解决方案

text PIP:“无法卸载'ipython'。这是一个distutils安装的项目,因此我们无法准确确定...“

更改级别记录到 IPython/Jupyter 笔记本

如何从 ipython qtconsole 使用标准库“记录”记录到文件?