what is new with int conversion in Python 3
Peter Otten
__peter__ at web.de
Sun May 22 04:32:48 EDT 2016
Sayth Renshaw wrote:
Read carefully:
> Run from the command line as follows
>
> python vectorsum.py n
>
> where n is an integer that specifies the size of the vectors.
So to run the script with Python 3 you could do
$ python3 vectorsum.py 42
in the shell. This implicitly sets sys.argv[1] to "42" so that the
conversion
size = int(sys.argv[1])
can succeed. This conversion failed because as the traceback indicates
> ValueError Traceback (most recent call
last)
> <ipython-input-8-a54a878f293d> in <module>()
> 37 return c
> 38
> ---> 39 size = int(sys.argv[1])
> 40
> 41 start = datetime.now()
>
> ValueError: invalid literal for int() with base 10: '-f'
>
sys.argv[1] is "-f" which is not a valid (base-10) integer. This value
probably got into ipython3 because you invoked it with something like
$ ipython3 console -f foo
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import vectorsum
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-b0b6e1aba9f9> in <module>()
----> 1 import vectorsum
/home/petto/vectorsum.py in <module>()
37 return c
38
---> 39 size = int(sys.argv[1])
40
41 start = datetime.now()
ValueError: invalid literal for int() with base 10: '-f'
In [2]: ! python3 vectorsum.py 42
The last 2 elements of the sum [65600, 70602]
PythonSum elapsed time in microseconds 106
The last 2 elements of the sum [65600 70602]
NumPySum elapsed time in microseconds 121
By the way, I had to fix another problem to make the "In [2]:" invocation
work. Let's see if you can do that yourself.
More information about the Python-list
mailing list