Jump to content


Photo

Head First Prgramming


  • Please log in to reply
11 replies to this topic

#1 BIG JG

BIG JG

    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 January 2010 - 10:06 AM

Is there going to be a dedicated Forum for this Book? Thanks

#2 Wanderling

Wanderling

    New Member

  • Members
  • Pip
  • 2 posts

Posted 12 January 2010 - 06:22 AM

Recently bought this book. I'm really liking it so far. Just came across an....issue of sorts though. On pages 58,59 the code is set to search for '>$' in the text.find line. This however seems to be pulling in the '>$' in the HTML validation header I think. To fix this I changed the search parameters to 'g>$' and then changed 'where +2' to 'where +3'. This seemed to pull in the correct price position. Thought I'd post on here to let you all know and to let others that might be a bit confused when it brings back !DOC on the original find.


#3 Valoric

Valoric

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 January 2010 - 01:35 PM

Howdy, I was just wondering if anyone has gotten to page 167 and realized that for some reason your program doesn't read and identify different "surfers" . I've hit a complete brick wall & been struggling on this for at least a week. I can't read on because I keep coming back to same problem, inevitably get tired of it then take another break whilst getting distracted.
Please some admin or experienced member help me. I have exhausted everything I've learnt prior to page 167 so there's nothing I got except this forum. ohmy.gif

#4 Raven-sb

Raven-sb

    Active Member

  • Members
  • PipPip
  • 12 posts

Posted 16 January 2010 - 03:22 AM

QUOTE (Valoric @ Jan 12 2010, 01:35 PM) <{POST_SNAPBACK}>
Howdy, I was just wondering if anyone has gotten to page 167 and realized that for some reason your program doesn't read and identify different "surfers" . I've hit a complete brick wall & been struggling on this for at least a week. I can't read on because I keep coming back to same problem, inevitably get tired of it then take another break whilst getting distracted.
Please some admin or experienced member help me. I have exhausted everything I've learnt prior to page 167 so there's nothing I got except this forum. ohmy.gif



Hi there, I can help with this as I hit my head against a brick wall with this very issue. The problem for me was that that IDLE didn't unindent the code properly after the return(s) statement. Python 3 it seems is very particular about how code is indented.


To fix it I made sure that the 'surfers_f.close' statement was lined up with the
'if id2find == int(s['id]):' statement.

You'll come across this problem in the most of the excercises in Chapter 5 and for me it was always due to the way IDLE indented the code. Just one of Python's quirks I guess.

Hope this help.




#5 pickboy87

pickboy87

    New Member

  • Members
  • Pip
  • 4 posts
  • Gender:Male
  • Location:Minnesota

Posted 18 January 2010 - 02:41 PM

QUOTE (Valoric @ Jan 12 2010, 01:35 PM) <{POST_SNAPBACK}>
Howdy, I was just wondering if anyone has gotten to page 167 and realized that for some reason your program doesn't read and identify different "surfers" . I've hit a complete brick wall & been struggling on this for at least a week. I can't read on because I keep coming back to same problem, inevitably get tired of it then take another break whilst getting distracted.
Please some admin or experienced member help me. I have exhausted everything I've learnt prior to page 167 so there's nothing I got except this forum. ohmy.gif


I ran into this problem as well. It would only output the first surfer (101) and everything else would just return blank. I fixed it by moving the second surfers_f.close() back an additional indent:

CODE
def find_details(id2find):
    surfers_f = open("surfing_data.csv")
    for each_line in surfers_f:
        s = {}
        (s['id'], s['name'], s['country'], s['average'], s['board'],
         s['age']) = each_line.split(";")
        if id2find == int(s['id']):
            surfers_f.close()
            return(s)
    surfers_f.close()
    return({})


That fixed the problem. Hopefully that helps you. Python (IDLE) was goofy indenting things and it just needed to be aligned so that it wasn't in the loop block.

#6 KPnut

KPnut

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 January 2010 - 11:08 AM

I'm also stuck with this. I think my problem is that the find_details function isn't returning s correctly. When I use:

CODE
def find_details(id2find):
surfers_f = open('surfing_data.csv')
for each_line in surfers_f:
s = {}
(s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = each_line.split(';')
if id2find == int(s['id']):
print(s)


find_details(102)


it prints the correct details to the screen, so I know s is being created correctly.

However, when I replace print(s) with return(s) and add print(s) underneath find_details(102), I get an error message "NameError: name 's' is not defined". Code:

CODE
def find_details(id2find):
surfers_f = open('surfing_data.csv')
for each_line in surfers_f:
s = {}
(s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = each_line.split(';')
if id2find == int(s['id']):
return(s)


find_details(102)
print(s)


I have tried typing the code as per the book, and also copying the code examples from this forum, but I always get the NameError message!

Any help gratefully received!

#7 pickboy87

pickboy87

    New Member

  • Members
  • Pip
  • 4 posts
  • Gender:Male
  • Location:Minnesota

Posted 27 January 2010 - 03:30 PM

QUOTE (KPnut @ Jan 27 2010, 11:08 AM) <{POST_SNAPBACK}>
I'm also stuck with this. I think my problem is that the find_details function isn't returning s correctly. When I use:

[...various code...]

I have tried typing the code as per the book, and also copying the code examples from this forum, but I always get the NameError message!

Any help gratefully received!


From what I can tell, you're not closing out of the .csv file after opening it; which is most likely causing the error. Other than that, the code looks correct (from a quick glance at least).

#8 KPnut

KPnut

    New Member

  • Members
  • Pip
  • 3 posts

Posted 28 January 2010 - 02:09 AM

QUOTE (pickboy87 @ Jan 27 2010, 03:30 PM) <{POST_SNAPBACK}>
From what I can tell, you're not closing out of the .csv file after opening it; which is most likely causing the error. Other than that, the code looks correct (from a quick glance at least).


Hey,
Thanks for that. When I went through my code again this morning, fresh eyes found the problem - I'd returned (surfer), then tried to print a variable called s, which obviously didn't exist.
Kate.

#9 Stuart Midgley

Stuart Midgley

    New Member

  • Members
  • Pip
  • 2 posts

Posted 17 February 2010 - 12:23 AM

I'm having a problem as well. Whatever surfer ID I enter, I get that number returned as the value of everyline, ie:

Enter the id of the surfer: 102
ID: 102
Name: 102
Country: 102
Average: 102
Board type: 102
Age: 102


I think I've got the code exactly as printed in the book, but I can't see why it seems to be repeating the entered value as the result.

CODE
def find_details(id2find):
surfers_f = open("surfing_data.csv")
for each_line in surfers_f:
s = {}
(s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = each_line.split(";")
if id2find == int(s['id']):
surfers_f.close()
return(s)
surfers_f.close()
return({})


lookup_id = int(input("Enter the id of the surfer: "))
surfer = find_details(lookup_id)
if surfer:
print("ID: " + surfer['id'])
print("Name: " + surfer['id'])
print("Country: " + surfer['id'])
print("Average: " + surfer['id'])
print("Board type: " + surfer['id'])
print("Age: " + surfer['id'])

Edited by Stuart Midgley, 17 February 2010 - 12:35 AM.


#10 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 17 February 2010 - 06:03 AM

QUOTE (Stuart Midgley @ Feb 17 2010, 12:23 AM) <{POST_SNAPBACK}>
CODE
def find_details(id2find):
if surfer:
print("ID: " + surfer['id'])
print("Name: " + surfer['id'])
print("Country: " + surfer['id'])
print("Average: " + surfer['id'])
print("Board type: " + surfer['id'])
print("Age: " + surfer['id'])


You are going to kick yourself! The code in your function is fine, however, the code above prints out the same value for each line. The second print statement should print surfer['name'], the third surfer['country'] and so on. You were looking in the wrong place. Change those print statements and everything should work OK. It should look like this:

CODE

if surfer:
print("ID: " + surfer['id'])
print("Name: " + surfer['name'])
print("Country: " + surfer['country'])
print("Average: " + surfer['average'])
print("Board type: " + surfer['board'])
print("Age: " + surfer['age'])


This is one of those times where copying'n'pasting a line of code can trip you up. Hopefully, this fixes things for you!

Cheers, Paul.

#11 Stuart Midgley

Stuart Midgley

    New Member

  • Members
  • Pip
  • 2 posts

Posted 17 February 2010 - 02:16 PM

QUOTE (paulbarry @ Feb 17 2010, 06:03 AM) <{POST_SNAPBACK}>
You are going to kick yourself!


Aaaaaaaaaarrrrrggggghhhhhhhh!!!!!!!! **Head-desk**

I went through the function at least ten times, following the logic as well as the syntax and it all seemed good, so at least I got that bit right.

It's always the little details that kill you in this programming lark, I keep finding. I'm going off now to write out 100 times "I must not assume I have quoted the right variable."

Thanks Paul, I really appreciate it.

#12 Mahesh Deshreddy

Mahesh Deshreddy

    New Member

  • Members
  • Pip
  • 4 posts

Posted 14 February 2015 - 07:20 AM

[quote name='KPnut' timestamp='1264619300' post='16821']

Hi there,

Try this code, I have also received same Name Error message but luckily worked out!
Instead of surfer I have changed it to 's'.

def find_details(ID2Find):
    surfers_f = open("surfing_data.csv")
    for line in surfers_f:
        s = {}
        (s['ID'], s['Name'], s['Country'], s['Average Score'], s['Board Type'], s['Age']) = line.split(";")
        if ID2Find == int(s['ID']):
            surfers_f.close()
            return(s)
    surfers_f.close()
    return({})
lookup_id = int(input("Enter the ID of the surfer: "))
s = find_details(lookup_id)
if s:
    print("ID            : " + s['ID'])
    print("Name          : " + s['Name'])
    print("Country       : " + s['Country'])
    print("Average Score : " + s['Average Score'])
    print("Board Type    : " + s['Board Type'])
    print("Age           : " + s['Age'])

I hope this is helpful :rolleyes:

Edited by Mahesh Deshreddy, 14 February 2015 - 07:25 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users