Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > BOOK: Python Projects
|
BOOK: Python Projects
This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2015, 10:53 AM
Registered User
 
Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
Default P19, "even squares", code error

On page 19, generator expressions are introduced. The following code from that page produces the expexted result in the form of a list of even squares:
Code:
>>> [n*n for n in range(1,11) if not n*n % 2]
[4, 16, 36, 64, 100]
At the bottom of the page there is an example of how to generate an identical result with a conventional for loop. However, the sample code produces odd integers, instead of even squares, because not and n*n are missing:
Code:
>>> result = []
>>> for n in range(1,11):
...     if n*n % 2:              # Missing not
...         result.append(n)     # n instead of n*n
... 
>>> print(result)
[1, 3, 5, 7, 9]
This code produces the expected result:
Code:
>>> result = []
>>> for n in range(1,11):
...     if not n*n % 2:
...         result.append(n*n)
... 
>>> print(result)
[4, 16, 36, 64, 100]
The Following User Says Thank You to Egalth For This Useful Post:
Yehuda Katz (June 26th, 2017)
 
Old October 17th, 2015, 11:02 AM
Registered User
 
Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
Default

Sorry folks, forgot to mention that this has already been addressed in the Errata section. However, the correction in the Errata section only mentions the missing not so the result is even integers, instead of even squares:
Code:
>>> result = []
>>> for n in range(1,11):
...     if not n*n % 2:          # "not" now included.
...         result.append(n)     # n*n still missing.
... 
>>> print(result)
[2, 4, 6, 8, 10]                   # Even integers instead of even squares.

Last edited by Egalth; October 17th, 2015 at 11:13 AM..
The Following User Says Thank You to Egalth For This Useful Post:
Yehuda Katz (June 26th, 2017)
 
Old October 17th, 2015, 12:21 PM
Wrox Author
 
Join Date: Feb 2015
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That's for spotting that. I can only apologize for the error, not sure how that got
through all the reviews but it obviously did. Your changes are absolutely correct.

Apologies again,

Alan G.
The Following User Says Thank You to Alan G For This Useful Post:
Egalth (October 19th, 2015)
 
Old June 26th, 2017, 06:18 PM
Registered User
 
Join Date: Jun 2017
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
Default Not real bugs but...

I tried to find in the index 'list comprehension' and 'conditional expression'. Not available...
 
Old June 26th, 2017, 08:40 PM
Wrox Author
 
Join Date: Feb 2015
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You are quite right, I'm not sure why they got missed out. I could say it's because the first chapter is supposed top be revision rather than anything new, but they have included loops so i suspect it is just a simple omission. Index entries are always a bit subjective because you can't index everything but those two probably should be there.

I can only apologize.





Similar Threads
Thread Thread Starter Forum Replies Last Post
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Error in sample code "Professional Codeigniter" langithitam BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 5 November 1st, 2009 06:20 AM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 22nd, 2009 11:12 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.