Reputation: 1
I've just started working with Python and I have been following a OCR GCSE Computing book. The code in the book I am supposed to copy is this:
#valid_response="True"
while True:
answer=(input("Press A, R, F or Q: "))
if answer in("A", "a"):
write_file()
elif answer in ("R", "r"):
read_file()
elif answer in ("F", "f"):
find_rec()
elif answer in ("Q", "q"):
break
else:
print("Invalid Response")
#function to write music details to file
def write_file():
"""Write details to the file"""
#create a new file or add to an existing file
f = open("music2.txt","a")
print ("Enter Details")
ok=True
while ok==True:
serial = (input("Enter Serial "))
f.write(serial+"\n")
genre = (input("Enter Genre "))
f.write(genre+"\n")
artist = (input("Enter Artist "))
f.write(artist+"\n")
title = (input("Enter Title "))
f.write(title+"\n")
ask=(input("Another? "))
if ask in ('y', 'ye', 'yes', 'Y'):
ok=True
else:
ok=False
f.close()
All I get when I run it is that the error "name write_file is not defined".
As far as I can make out, i have defined it in the code. Im using 3.3.4 and wondered if I have missed anything obvious.
Thank you
Upvotes: 0
Views: 168
Reputation: 11190
Move the function above the function call. Your main script should always be on the bottom.
Upvotes: 1