简体   繁体   中英

Can't figure out if/else syntax in Python

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 :)

You have a missing parantheses on the line above the print line -

readname = raw_input("Type the filename of the file you want to read here -->"
                                                                              ^ 
                                                            Parantheses missing

It should be -

readname = raw_input("Type the filename of the file you want to read here -->")

In addition to the missing parentheses noted by the other answer, you also have a problem 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)

That is, you define findname but later try to use filename , which hasn't been defined.

I also have a couple suggestions you might want to look into:

  • Use a tool like flake8 to give you suggestions regarding your code (this will try to help you ensure your code complies with PEP8 , the Python coding style guideline. Though it won't catch every error in your code.)
  • Try using an IDE for real-time feedback on your code. There are many available ; I personally prefer PyCharm .

Here's an example of flake8 's output:

$ flake8 orig.py
orig.py:1:80: E501 line too long (92 > 79 characters)
orig.py:5:80: E501 line too long (82 > 79 characters)
orig.py:6:10: E901 SyntaxError: invalid syntax
orig.py:9:80: E501 line too long (86 > 79 characters)
orig.py:16:1: W391 blank line at end of file
orig.py:17:1: E901 TokenError: EOF in multi-line statement

You have not inserted closing bracket at this line:

    readname = raw_input("Type the filename of the file you want to read here -->"

replace this by :

    readname = raw_input("Type the filename of the file you want to read here -->"

and use print("") instead of print

   print("Your message here")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM