python read output -
write program outputs first number within file specified user. should behave like:
enter file name: l11-1.txt
first number 20.
you need use file object method .read(1) read 1 character @ time, , string object method check if number. if there no number, expected behaviour is:
enter file name: l11-2.txt
there no number in l11-2.txt.
why reading 1 character @ time better algorithm calling .read() once , processing resulting string using loop?
i have files , correspond answers above im not sure how make output properly.
the code have far below:
filenm = raw_input("enter file name: ") datain=file(filenm,"r") try: c=datain.read(1) result = [] while int(c) >= 0: result.append(c) c = datain.read(1) except: pass if len(result) > 0: print "the first number is",(" ".join(result))+" . " else: print "there no number in" , filenm + "."
so far opens file , reads output no number if there one. can me ?
ok, you've been given instructions:
- read string input user
- open file given string
.read(1)
character @ time until first number or eof- print number
you've got first , second parts here (although should use open
instead of file
open file), next? first thing work out algorithm: want computer do?
Comments
Post a Comment