python - Print won't work in if statement -
when run script, skips print function in line 8. cannot figure out why life of me. have tried many things working can't seem figure out problem here. new python excuse me if it's simple issue.
edit: woops, forgot actual code! facepalm here is:
import webbrowser import sys b = webbrowser.get('windows-default') print('type start') line1 = sys.stdin.readline() start = 'start' if line1 == start: print('what website want open?') line2 = sys.stdin.readline() b.open(line2)
when type 'start'
stdin , nail enter, entire string including newline character ends beingness stored in line1
. in reality, line1 == 'start\n'
. need remove \n
end of string before comparison. easy way using str.rstrip
:
if line1.rstrip() == start: print('what website want open?')
edit:
as ashwini chaudhary pointed out in comments, should using raw_input
(or input
if using python 3.x) instead of sys.stdin.readline
. create code shorter, , remove need strip trailing newline altogether:
line1 = raw_input('type start') start = 'start' if line1 == start: line2 = raw_input('what website want open?') b.open(line2)
python if-statement printing
No comments:
Post a Comment