Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    3
    ReputationRep:
    Hi!

    So basically, I need to read separate lines from a text file depending on the user's input. I can read the whole text file and all but, I need to make my code a bit more efficient so instead of having like 5 separate text files , I need to have one with all the info.

    So here is an example:

    This is the content of the text file 'all_Colours':

    1.Primary colours make up secondary colours
    2.Colours are everywhere.
    3.Wolfs are cool
    4.Fruits are good for you.

    Python Code:

    primaryColours=['red', 'blue' , 'green' ]

    colour=input("What is your fav colour?" )

    if colour in primaryColours:
    print("Here are some fun facts:" )
    text_file=open("all_Colours.txt" , "r" )
    print (text_file.read()) ## Instead of reading the whole text file I only want to read lines 1 and 2. How do i do this??????
    text_file.close()

    Thank you for all the help
    Offline

    16
    ReputationRep:
    What language is this?
    Offline

    2
    ReputationRep:
    (Original post by The_JoKeR)
    What language is this?
    It seems to be heavily, and incorrectly punctuated English.
    • Thread Starter
    Offline

    3
    ReputationRep:
    oh python

    Sorry, i'm a beginner, ignore my many mistakes. I know it's not perfect but i'm looking to improve and just figure this out.
    Offline

    16
    ReputationRep:
    (Original post by bluepearl7)
    oh python

    Sorry, i'm a beginner, ignore my many mistakes. I know it's not perfect but i'm looking to improve and just figure this out.
    Ah sorry no help there then. Did Visual Basic and Javascript instead.
    Offline

    3
    ReputationRep:
    (Original post by bluepearl7)
    ## Instead of reading the whole text file I only want to read lines 1 and 2. How do i do this??????
    Code:
    text_file.readlines()[0:2]
    Offline

    18
    ReputationRep:
    OP are you self studying or what ?

    Posted from TSR Mobile
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by Push_More_Button)
    Code:
    text_file.readlines()[0:2]
    Thankyou!
    I get it now.

    (Original post by nerdling_CompSci)
    OP are you self studying or what ?

    Posted from TSR Mobile
    Not really. I'm a student trying to do coursework.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Would you like to hibernate through the winter months?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.