Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:48:24+00:00 2026-05-23T11:48:24+00:00

I’m not going to lie. I’m trying to do an assignment and I’m being

  • 0

I’m not going to lie. I’m trying to do an assignment and I’m being beaten by it.

I need to have python prompt the user to enter a room number, then lookup that room number in a supplied .txt file which has csv [comma-separated values], and then show multiple results if there are any.

I was able to get python to return the first result ok, but then it stops. I got around the csv thing by using a hash command and .split (I would rather read it as a csv although I couldn’t get it to work.) I had to edit the external file so instad of the data being seperated by commas it was seperated by semicolons, which is not ideal as I am not supposed to be messing with the supplied file.

Anyhow…
My external file looks like this:

roombookings.txt

6-3-07;L1;MSW001;1
6-3-07;L2;MSP201;1
6-3-07;L3;WEB201;1
6-3-07;L4;WEB101;1
6-3-07;L5;WEB101;1
7-3-07;L1;MSW001;2
7-3-07;L2;MSP201;2
7-3-07;L3;WEB201;2
7-3-07;L4;WEB101;2
7-3-07;L5;WEB101;2
8-3-07;L1;WEB101;1
8-3-07;L2;MSP201;3

Here’s what my code looks like:

roomNumber = (input("Enter the room number: "))


def find_details(id2find):
    rb_text = open('roombookings.txt', 'r')
    each_line = rb_text.readline()
    while each_line != '':
        s = {}
        (s['Date'], s['Room'], s['Course'], s['Stage']) = each_line.split(";")
        if id2find == (s['Room']):
                rb_text.close()
                return(s)
        each_line = rb_text.readline()
    rb_text.close()

room = find_details(roomNumber)
if room:
    print("Date: " + room['Date'])
    print("Room: " + room['Room'])
    print("Course: " + room['Course'])
    print("Stage: " + room['Stage'])

If i run the program, I get prompted for a room number. If I enter, say, “L1”
I get:

Date: 6-3-07
Room: L1
Course: MSW001
Stage: 1

I should get 3 positive matches. I guess my loop is broken? Please help me save my sanity!

Edit. I’ve tried the solutions here but keeps either crashing the program (I guess I’m not closing the file properly?) or giving me errors. I’ve seriously been trying to sort this for 2 days and keep in mind I’m at a VERY basic level. I’ve read multiple textbooks and done many Google searches but it’s all still beyond me, I’m afraid. I appreciate the assistance though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:48:25+00:00Added an answer on May 23, 2026 at 11:48 am

    First. For iterating over lines in the file use next:

    for line in rb_text:
        # do something
    

    Second. Your function returns after first match. How can it match more then one record? Maybe you need something like:

    def find_details(id2find):
        rb_text = open('roombookings.txt', 'r')
        for line in rb_text:
            s = {}
            (s['Date'], s['Room'], s['Course'], s['Stage']) = line.split(";")
            if id2find == (s['Room']):
                yield s
        rb_text.close()
    

    And then:

    for room in find_details(roomNumber):
        print("Date: " + room['Date'])
        print("Room: " + room['Room'])
        print("Course: " + room['Course'])
        print("Stage: " + room['Stage'])
    

    And yes, you better use some CSV parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.