Jump to content


Photo

Chapter 5_nameerror: Name 's' Is Not Defined_solved


  • Please log in to reply
No replies to this topic

#1 Mahesh Deshreddy

Mahesh Deshreddy

    New Member

  • Members
  • Pip
  • 4 posts

Posted 14 February 2015 - 07:37 AM

Hello Everyone,

When I wrote code as per book in page 167, I have got below error
Traceback (most recent call last):
  File "C:/Python_Head First_Projects/Chapter 5_Hashes and databases/Search and arrange for multiple lines_167_APB.py", line 14, in <module>
    print("ID            : " + s['ID'])
NameError: name 's' is not defined

And there was no proper solution in the forum so am posting my working code below, only thing I have done is replaced surfer with the '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'])

Hope you find it helpful. :P

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users