[Tutor] readline

Joel Goldstick joel.goldstick at gmail.com
Wed Jul 31 22:45:54 CEST 2013


On Wed, Jul 31, 2013 at 4:12 PM, Mike McTernan <livingmike at gmail.com> wrote:
> I am having problems with the readline command in Python 2.7.
>
> I have a text file that has 12 lines of text, each line represents a
> response to a variable called star_sign that is collected via
> raw_input.
>
> However, instead of printing the line 5 it prints the first 5 letters on line 1.
>
> Here is the program:
>
> from sys import argv
>
> script, zodiac = argv
>
> prediction = open(zodiac, "r")
>
> prompt = "> "
>
> def welcome():
>     print "Welcome to your daily horoscope"
>     horoscope()
>
> def horoscope():
>     print "I can predict your future"
>     print "What is your starsign?"
>     star_sign = raw_input(prompt)
>
>     if "Leo" in star_sign:
>         print prediction.readline(5)

The above reads only the first 5 characters from the file.  You
probably want to use foo  = readlines() which will read each line of a
file into foo.  Then print foo[5] or maybe foo[4].  I'm not sure what
you are doing

There is a good tutorial in the python docs on doing file stuff.  It
will help you understand this stuff better.
>
>     else:
>         print "You gonna die"
>
> welcome()
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list