Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23103

Python 4 Kids: Python for Kids: Python 3 – Project 7

$
0
0

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

In this post I talk about the changes that need to be made to the code of
Project 7 in order for it to work with Python 3. Most of the code in project 7 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 and it’s
those changes that I am mainly identifying below.

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 178

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

Page 179-180
The code and syntax on these pages is the same, but the outputs are
different in Python 3. This is because, in Python 3,
the range builtin does not create a list as in Python 2.7 (see Python3/Project 5)

#Python 2.7 code: 

>>> test_string = '0123456789'
>>> test_string[0:1]
'0'
>>> test_string[1:3]
'12'
>>> # range(10) is a list of the numbers from 0 to 9 inclusive
>>> range(10)[0:1]
[0]
>>> range(10)[1:3]
[1, 2]
>>> test_string[:3]
'012'
>>> test_string[3:]
'3456789'
#Python 3 code:
>>> test_string = '0123456789'
>>> test_string[0:1]
'0'
>>> test_string[1:3]
'12'
>>> # range(10) is no longer a list. Rather, it's a generator
>>> # so the [:] operator slices the generator. You can use list()
>>> # to see what list the generator corresponds to.
>>> range(10)[0:1]
range(0, 1)
>>> list(range(10)[0:1])
[0]
>>> # note same output as in Python 2.7
>>> range(10)[1:3]
range(1, 3)
>>> list(range(10)[1:3])
[1, 2]
>>> test_string[:3]
'012'
>>> test_string[3:]
'3456789'
>>>

Pages 180-196
All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7

Page 199

The code on this page uses raw_input, which has been renamed to input in Python 3.
Either change all occurrences or add a line

raw_input = input

at the start of the relevant code.

#Python 2.7 code:
#### Input and Output Section
message = raw_input("Type the message to process below:\n")
ciphertext = encrypt_msg(message, ENCRYPTION_DICT)
plaintext = decrypt_msg(message, DECRYPTION_DICT)
print("This message encrypts to")
print(ciphertext)
print # just a blank line for readability
print("This message decrypts to")
print(plaintext)

#Python 3 code:
#### Input and Output Section
message = input("Type the message to process below:\n")
ciphertext = encrypt_msg(message, ENCRYPTION_DICT)
plaintext = decrypt_msg(message, DECRYPTION_DICT)
print("This message encrypts to")
print(ciphertext)
print # just a blank line for readability
print("This message decrypts to")
print(plaintext)

>>> ================================== RESTART ================================
>>>
Type the message you'd like to encrypt below:
I love learning Python. And my teacher is smelly. And I shouldn't start a sentence with and.
This message encrypts to
F|ilsb|ib7okfkd|Mvqelk+|xka|jv|qb79ebo|fp|pjbiiv+||xka|F|pelriak$q|pq7oq|7|pbkqbk9b|tfqe|7ka+
This message decrypts to
L2oryh2ohduqlqj2SBwkrq;2Dqg2pB2whdfkhu2lv2vphooB;22Dqg2L2vkrxogq*w2vwduw2d2vhqwhqfh2zlwk2dqg;

>>> ================================== RESTART ================================
>>>
Type the message you'd like to encrypt below:
F|ilsb|ib7okfkd|Mvqelk+|xka|jv|qb79ebo|fp|pjbiiv+||xka|F|pelriak$q|pq7oq|7|pbkqbk9b|tfqe|7ka+
This message encrypts to
C_fip8_f84lhcha_Jsnbih(_uh7_gs_n846b8l_cm_mg8ffs(__uh7_C_mbiof7h!n_mn4ln_4_m8hn8h68_qcnb_4h7(
This message decrypts to
I love learning Python. And my teacher is smelly.  And I shouldn't start a sentence with and.

Page 200

This code works as is in both Python 2.7 and Python 3. However, the way the open() builtin works
has changed in Python 3 and this will cause some issues in later projects. In Python 3 open()
has the same syntax as in Python 2.7, but uses a different way to
get data out of the file and into your hands. As a practical matter this means that some Python 2.7
code will sometimes cause problems when run in Python 3. If you run into such a problem (open code that works
in Python 2.7 but fails in Python 3), the first thing to try is to add the binary modifier. So,
instead of ‘r’ or ‘w’ for read and write use ‘rb’ or ‘wb’. This code doesn’t need the binary modifier, but a later project will.

Page 201

The code on this page is the same, but the outputs are different in Python 3. Python 3 returns how much data has
been written (in this case, 36)

#Python 2.7 code:
>>> file_object = open('p4k_test.py','w')
>>> text = "print('Hello from within the file')\n" # watch the " and '
>>> file_object.write(text) # writes it to the file
>>> file_object.write(text) # writes it to the file again!
>>> file_object.close() # finished with file, so close it
#Python 3 code:
>>> file_object = open('p4k_test.py','w')
>>> text = "print('Hello from within the file')\n" # watch the " and '
>>> file_object.write(text) # writes it to the file
36
>>> file_object.write(text) # writes it to the file again!
36
>>> file_object.close() # finished with file, so close it

######## Pages 202 and 203

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

######## Page 204

All code on this page is the same in Python 3 as in Python 2.7, but some of the outputs are different
A line has been added in the Python 3 code below to shown that the file_object has been closed after leaving the
with clause – this was explicit in the print out in Python 2.7. Also, because Python 3 uses a different way
of getting information from a file it is identified differently. In Python 2.7 it’s call a file – pretty straight
forward. in Python 3 it’s called a _io.TextIOWrapper. Not as enlightening, but a student doesn’t need to worry about
this difference in detail.

>>> #Python 2.7
>>> with open('p4k_test.py','r') as file_object:
        print(file_object.read())

print('Hello from within the file')
print('Hello from within the file')

>>> file_object
<closed file 'p4k_test.py', mode 'r' at 0xf7fed0>

>>> #Python 3
>>> with open('p4k_test.py','r') as file_object:
        print(file_object.read())

print('Hello from within the file')
print('Hello from within the file')

>>> file_object  # output different from 2.7
<_io.TextIOWrapper name='p4k_test.py' mode='r' encoding='UTF-8'>

>>> file_object.closed # but the file is still closed
True

######## Page 205

All code on this page is the same in Python 3 as in Python 2.7, but some of the outputs are different
A line has been added in the Python 3 code below to shown that the file_object has been closed after leaving the
with clause – this was explicit in the print out in Python 2.7.

>>> #Python 2.7
>>> with open('testfile2','w') as a:
        a.write('stuff')

>>> with open('testfile2','r') as a,
         open('p4k_test.py','r') as b:
        print(a.read())
        print(b.read())

stuff
print('Hello from within the file')
print('Hello from within the file')

>>> a
<closed file 'testfile2', mode 'r' at 0xf6e540>
>>> b
<closed file 'p4k_test.py', mode 'r' at 0xef4ed0>

>>> #Python 3
>>> with open('testfile2','r') as a, open('p4k_test.py','r') as b:
	print(a.read())
	print(b.read())

stuff
print('Hello from within the file')
print('Hello from within the file')

>>> a
<_io.TextIOWrapper name='testfile2' mode='r' encoding='UTF-8'>
>>> a.closed
True
>>> b
<_io.TextIOWrapper name='p4k_test.py' mode='r' encoding='UTF-8'>
>>> b.closed
True

######## Page 207

All code on this page is the same in Python 3 as in Python 2.7, but some of the outputs are different
(the write method returns the amount of data written and this is output in the console in Python 3)

>>> #Python 2.7
>>> INPUT_FILE_NAME = "cryptopy_input.txt"
>>> with open(INPUT_FILE_NAME,'w') as input_file:
        input_file.write('This is some test text')

>>> #Python 3>>> INPUT_FILE_NAME = "cryptopy_input.txt"
>>> with open(INPUT_FILE_NAME,'w') as input_file:
	input_file.write('This is some test text')

22

# this code is the same in Python 2.7 and Python 3:

INPUT_FILE_NAME = “cryptopy_input.txt”
OUTPUT_FILE_NAME = “cryptopy_output.txt”

######## Page 208 -218
All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7



Viewing all articles
Browse latest Browse all 23103

Trending Articles