I've decided to learn how to program, and because it's what everyone recommended first, I've started working out Python. I've learned what I think are the basics, recently figuring out if/else statements. I thought as a little challenge I might try to apply most the things I've learned and whip up a little program that did something. So I'm trying to make a script that can read a file or find if a specific word is in a file, giving the user a choice. Here's the code I wrote, and isn't working.
print "Hello, would you like to read a file or find whether or not some text is in a file?"
choice = raw_input("Type 'read' or 'find' here --> ")
if choice == "read":
readname = raw_input("Type the filename of the file you want to read here -->"
print open(readname).read()
elif choice == "find":
word = raw_input("Type the word you want to find here --> ")
findname = raw_input("Type the filename of the file you want to search here --> ")
if word in open(findname).read():
print "The word %r IS in the file %r" % (word, filename)
else:
print "The word %r IS NOT in the file %r" % (word, filename)
else:
print "Sorry, don't understand that."
I'm a total scrub and you could probably tell that by looking at the code, but anyway help would be appreciated. Firstly, Python is giving me a syntax error right on print. It doesn't give me the error when I mark out the variable line above it, so I suppose there's a problem there, but I can't find anything in the Internet. Also, if I mark out the variable line like I said but type "find" when I run it (running the elif portion) I get an error saying that findname isn't defined, but I can't find why it wouldn't? Anyway, I'm sure it's blatantly obvious but hey, I'm learning, and I'd love if any of you would tell me why this code sucks :)