Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Using Python 3 in Project 8 of Python For Kids For Dummies

$
0
0
Using python 3 in Project 8 of Python For Kids ForDummies

August 28, 2016 Leave a comment

In this post I talk about the changes that need to be made to the code of

Project 8 in order for it to work with Python 3. Happily, the code in Project 8 will work without changes.

However, in some places what Python outputs in Python 3 is different (usually just a little different) from the output in Python 2.7. The code below shows where the output is different.

Disclaimer

Some people want to use my book Python for Kids for Dummies to learn Python 3.

I am working through the code in the existing book, highlighting changes from Python 2 to Python 3

and providing code that will work in Python 3. If you are using Python 2.7 you can ignore this post.

This post is only for people who want to take the code in my book Python for Kids for Dummies and

run it in Python 3.

Page 220

All code on this page is the same, and all outputs from the code are the same in Python 3 as in Python 2.7.

Page 222

All code syntax on page is the same, but some outputs are different in Python 3 different output from type().

#Python 2.7
>>> "%s %s"%(1,2)
'1 2'
"%s %s"%(1) #(two specifiers, one value)
"%s %s"%(1,2,3) #(two specifiers, three values)
>>> values = (1,2)
>>> "%s %s"%values
'1 2'
>>> # Snuck in a tuple:
>>> type(values)
<type 'tuple'> #Python 3
>>> "%s %s"%(1,2)
'1 2'
"%s %s"%(1) #(two specifiers, one value)
"%s %s"%(1,2,3) #(two specifiers, three values)
>>> values = (1,2)
>>> "%s %s"%values
'1 2'
>>> type(values)
<class 'tuple'>
>>>

Page 223 224

All code on this page is the same, and all outputs from the code are the same in Python 3 as in Python 2.7.

Page 225

All code syntax on page is the same, but some outputs are different in Python 3 different error messages.

#Python 2.7
>>> def test_function():
return (1,2,3) # returns a tuple with three elements
>>> a = test_function()
>>> a
(1, 2, 3)
>>> a,b,c = test_function()
>>> a
1
>>> b
2
>>> c
3
>>> a,b = test_function()
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
a,b = test_function()
ValueError: too many values to unpack
>>> a,b,c = (1,2,3) # unpack the tuple into a, b, c
>>> print("a: %s, b: %s, c: %s"%(a,b,c))
a: 1, b: 2, c: 3
>>> a,b = (1,2,3) # three values but only two variables.
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
a,b = (1,2,3)
ValueError: too many values to unpack #Python 3
>>> def test_function():
return (1,2,3) # returns a tuple with three elements
>>> a = test_function()
>>> a
(1, 2, 3)
>>> a,b,c = test_function()
>>> a
1
>>> b
2
>>> c
3
>>> a,b = test_function()
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
a,b = test_function()
ValueError: too many values to unpack (expected 2)
>>> a,b,c = (1,2,3) # unpack the tuple into a, b, c
>>> print("a: %s, b: %s, c: %s"%(a,b,c))
a: 1, b: 2, c: 3
>>> a,b = (1,2,3) # three values but only two variables.
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
a,b = (1,2,3) # three values but only two variables.
ValueError: too many values to unpack (expected 2)

Page 227-232

All code on these pages is the same, and all outputs from the code are the same in Python 3 as in Python 2.7.


Viewing all articles
Browse latest Browse all 9596

Trending Articles