How to write in python "files" , program ask user how many name they like to write to a file with name file with names and the ask to write one name at each time and validate number entered is greater than zero before entering the loop
How to write in python "files" , program ask user how many name they like to write to a file with name file with names and the ask to write one name at each time and validate number entered is greater than zero before entering the loop
Algorithm: Writing Names to a File with User Input Validation
1. Initialize a variable num_names to 0.
2. Repeat the following until a valid number of names is entered:
a. Display a prompt to the user, asking how many names they would like to write to a file.
b. Try to read user input as an integer:
- If successful, store the entered value in num_names.
- If the value is greater than 0, exit the loop.
- If the value is not greater than 0, display an error message and repeat the loop.
- If the user enters a non-integer input, catch the ValueError and display an error message.
3. Set the file_name variable to "file_with_names.txt".
4. Open a file named file_name for writing and create a new file (if it doesn't exist) with the "w" mode. Use a context manager to ensure proper file handling.
5. Repeat for i from 1 to num_names:
a. Display a prompt asking the user to enter a name (e.g., "Enter name 1", "Enter name 2", etc.).
b. Read the user's input as a name.
c. Write the entered name to the file, followed by a newline character "\n", to separate each name on a new line.
6. Close the file to save the changes.
7. Display a confirmation message, indicating the number of names that have been written to the file.
8. End.
Step by step
Solved in 4 steps with 1 images
for i in range
whats i mean and how program use it as defined letter
And for {} is there way to do it with () {i+1}