Can't figure out if/else syntax in Python -


i've decided learn how program, , because it's recommended first, i've started working out python. i've learned think basics, figuring out if/else statements. thought little challenge might try apply things i've learned , whip little program did something. i'm trying make script can read file or find if specific word in file, giving user choice. here's code wrote, , isn't working.

print "hello, read file or find  whether or not text in file?" choice = raw_input("type 'read' or 'find' here --> ")  if choice == "read":     readname = raw_input("type filename of file want read here -->"     print open(readname).read() elif choice == "find":     word = raw_input("type word want find here --> ")     findname = raw_input("type filename of file want search here --> ")     if word in open(findname).read():         print "the word %r in file %r" % (word, filename)     else:         print "the word %r not in file %r" % (word, filename) else:     print "sorry,  don't understand that." 

i'm total scrub , tell looking @ code, anyway appreciated. firstly, python giving me syntax error right on print. doesn't give me error when mark out variable line above it, suppose there's problem there, can't find in internet. also, if mark out variable line said type "find" when run (running elif portion) error saying findname isn't defined, can't find why wouldn't? anyway, i'm sure it's blatantly obvious hey, i'm learning, , i'd love if of tell me why code sucks :)

in addition missing parentheses noted other answer, have problem here:

findname = raw_input("type filename of file want search here --> ") if word in open(findname).read():     print "the word %r in file %r" % (word, filename) else:     print "the word %r not in file %r" % (word, filename) 

that is, define findname later try use filename, hasn't been defined.

i have couple suggestions might want into:

  • use tool flake8 give suggestions regarding code (this try ensure code complies pep8, python coding style guideline. though won't catch every error in code.)
  • try using ide real-time feedback on code. there many available; prefer pycharm.

here's example of flake8's output:

$ flake8 orig.py orig.py:1:80: e501 line long (92 > 79 characters) orig.py:5:80: e501 line long (82 > 79 characters) orig.py:6:10: e901 syntaxerror: invalid syntax orig.py:9:80: e501 line long (86 > 79 characters) orig.py:16:1: w391 blank line @ end of file orig.py:17:1: e901 tokenerror: eof in multi-line statement 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -