Wrox Programmer Forums P19, "even squares", code error
 Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Python Projects
 This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2 Read more about Python Projects or buy the book from your favorite retailerDownload the code for Python Projects
 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
October 17th, 2015, 10:53 AM
Registered User
 Points: 46, Level: 1
 Activity: 0%

Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
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)
October 17th, 2015, 11:02 AM
Registered User
 Points: 46, Level: 1
 Activity: 0%

Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts

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)
October 17th, 2015, 12:21 PM
Wrox Author
 Points: 119, Level: 2
 Activity: 0%

Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts

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)
June 26th, 2017, 06:18 PM
Registered User
 Points: 3, Level: 1
 Activity: 0%

Join Date: Jun 2017
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
Not real bugs but...

I tried to find in the index 'list comprehension' and 'conditional expression'. Not available...
June 26th, 2017, 08:40 PM
Wrox Author
 Points: 119, Level: 2
 Activity: 0%

Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts

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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 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

All times are GMT -4. The time now is 12:15 AM.