Python for Kids: Python 3 – Project 8


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

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. Most of the code in project 8 will work without changes.
However, in a lot of cases what Python outputs in Python 3 is different from the output in Python 2.7.

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: