Skip to content

This repository includes python code that creates files with multiple lines of user-entered text and handles errors from invalid input.

Notifications You must be signed in to change notification settings

aieckxis/multiple-line-of-text-contents

Repository files navigation

Write multiple line of text contents into a text file

This Python script creates a file with multiple lines of text in it. As long as the user does not say there are any more lines, it asks them to enter text lines and writes them to the file. The code has error handling to deal with incorrect user input, making sure that the program keeps asking the user for input until a valid one is entered.

Usage

Using Command Prompt:

  1. Open the Start menu and search for "Command Prompt".
  2. Click on "Command Prompt" to open the command prompt window.
  3. Use the cd command to navigate to the directory containing the script file. For example, if the script file is located in the "Documents" folder, type: cd Documents
  4. Type the command python script_name.py to run the script. Replace "script_name.py" with the actual name of the script file.
  5. Press Enter to run the script.
  6. The script will run and ask the user to input their name and the line and if they will add more lines.

Alternatively, you can also run the script using the Python IDLE environment:

  1. Open the Start menu and search for "Python".
  2. Click on "Python" to open the Python IDLE environment.
  3. Click on "File" at the top of the window and select "Open".
  4. Navigate to the directory containing the script file and select it.
  5. Click on "Run" at the top of the window and select "Run Module" or press the F5 key.
  6. The script will run and ask the user to input their name and the line and if they will add more lines.

Example

Suppose that the user enter their name as Kate:

image

and enter the line shown in the image below:

image

The program will then ask the user if there are more lines.

image

If the user entered the letter 'y', the program will ask the user to enter new line.

image

but if the user entered the letter 'n', the program will exit the loop and the resulting text file "mylife.txt" contains the following lines of text:

image

Code Explanation

image

This code allows the user to enter multiple lines of text and writes them to a file called "mylife.txt". The line number 5 of code sets the filename as "mylife.txt". The with statement is used to ensure that the file is properly closed after it is opened. This also makes the code more concise. The open() function is called with the file name and the "w" mode parameter, which means that the file is opened in write mode.

image

The program asked the user to enter their first name to be add in the variable of add_line. A while loop is used to continue asking the user for input until they indicate that there are no more lines to enter. The user is also asked to enter a line of text with the input() function, and the value is stored in the variable line. The write() method is used to add the line of text to the file, followed by a newline character (\n). The user is then prompted to indicate if there are more lines to be entered with the input() function, and the value is stored in the variable add_line.

image

If the user enters "n", the break statement is used to exit the while loop. Or else, if the user enters "y", the continue statement is used to return to the beginning of the loop and prompt the user for another line of text. And if the user enters anything else, an error message is displayed and the loop is repeated until valid input is provided.

Potential Improvements

  • Adding error handling to the code will make it more robust and prevent crashes caused by unexpected user input.
  • The program could be improved by adding error handling to handle unexpected input from the user. Additionally, it would be helpful to allow the user to input the name of the file they want to write to, rather than always writing to a file named "mylife.txt".
  • The way the program interacts with the user could be improved to make it easier to use. For example, it could provide better instructions or let the user quit at any time.

Conclusion

In conclusion, the program enables users to enter lines of text and save them to a chosen file. The program's functionality could be improved by adding more advanced features to the relatively simple code, which currently functions as intended. However, the program is a helpful tool for those who want to quickly and easily create and save text files.

About

This repository includes python code that creates files with multiple lines of user-entered text and handles errors from invalid input.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages