(PYTHON)
Write a function to read the content of a text file and print it
out. There are 6 different text files, and there can be two types
of input, such as mtrline1 and mtrline1_r.txt (the program should
be able to read both types). When the input is, for example,
mtrline2_r.txt then the program should output the contents in the
text file named "line2.txt".
This should be operated after a function, and so technically,
when the input is like below:
[line 1]
[station 1]
[station 2]
[station 3]
[-1] ---> (this is an input which will stop asking for the
input for station names)
[mtrline3_r.txt]
Then, the program will give below:
[Line 1]: station 1 <-> station 2 <-> station 3
[Line 2]: (contents in line3.txt file)
I am done with the function for Line 1, and I am working on the
Line 2 function. So the part with the texts with bold font is where
I need to complete. Thank you
1. Write a function to ask user for the name of Line 1 (e.g., Island). After that, ask user for names of stations on Line 1. You should keep asking until a stop code -1' is detected. 2. Write another function to read the content of a text file (line2.txt). The function should be able to read the text file even if the default file extension (.txt) is missing in the input. The text file has the following format. The first line is the name of Line 2, and the following lines are the names of stations on Line 2 (one station name per line). You can assume that there is at least one station on a line. tsuen wan Central admiralty Tsim Sha Tsui 3. Print Line 1 and Line 2. The station names and line names should be printed in title cases, i.e., if you encounter 'tsuen wan' as an input, you should convert it into 'Tsuen Wan’. You are not allowed to use the .title() method.
Inputs: Name of Line 1, terminated by a newline ('\n') character Continuous entry of names of stations on Line 1, each name is terminated by a newline (\n') character. Stop when -1' is inputted Filename of the text file (.txt) for Line 2, terminated by a newline ('\n') character . Outputs: . Line 1, terminated by a newline ('\n') character Line 2, terminated by a newline ('\n') character Format: [line name] line: [station name 1]<-> [station name 2]<->... <-> [station name N]
Sample output Island line: HKU<->Sai Ying Pun<->Sheung Wan<->Central Tsuen Wan line: Central<->Admiralty<->Tsim Sha Tsui Examples: Case Sample input 1 Island hku Sai ying Pun sheung wan Central -1 line 2 2 N South Island line: Wong Chuk Hang<->Ocean Park<->Admiralty Tsuen Wan line: Central<->Admiralty<->Tsim Sha Tsui south island Wong Chuk Hang Ocean park admiralty -1 line 2.txt The content of text files we used in the examples are attached at the end of this document.
line1.txt island HKU Sai Ying pun sheung wan central Admiralty Wan chai Causeway bay line2.txt Tsuen Wan Central admiralty Tsim Sha Tsui line3.txt south island wong chuk hang Ocean Park Admiralty line4.txt tuen ma austin east Tsim Sha tsui hung hom Ho Man Tin line5.txt East rail tai wai Kowloon Tong Mong kok east hung Hom line6.txt Tuen Ma tai wai hin keng Diamond hill kai tak Sung Wong Toi to kwa Wan họ man tin Hung Hom
1. Write a function to ask user for the name of Line 1 (e.g., Island). After that, ask user for names of stations on Lin
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
1. Write a function to ask user for the name of Line 1 (e.g., Island). After that, ask user for names of stations on Lin
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!