Question

In: Computer Science

Using python Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user. The write_to_file...

Using python

Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user.

The write_to_file function should have 2 parameters, file_name and data. When called, the function will open a file with the name stored in the file_name variable, write the information stored in data, then close the file.

The read_from_file function will have 1 parameter, file_name. When called, the function will open a file with the name stored in the file_name variable, print the contents of the file, then close the file.

The ask_user function will give the user a prompt asking whether they want to read or write a file, once a selection is made the program must ask the user for the information needed to run the function (if they select write, ask for file name and data to write to the file, if the select read, ask for file name).

Below the functions, in the main part of the program, write a for loop that loops 10 times calling the ask_user function.

Solutions

Expert Solution

def write_to_file(name,data):
  #opening the file and wrtiing into it
        file = open(name,"w")
        file.write(data)
        file.close()
def read_from(name):
  #opening file to read data
        file = open(name)
  #reading line by line
        for x in file:
                print(x)
        file.close()
def ask_user():
  #asking the user to choose 
        ch=int(input(("Please choose\n1 Write to File\n2 Read from file\n")))
        #reading user input according to choice
  if ch==1:
                name=input("Enter file name: ")
                data=input("Enter data to write")
                write_to_file(name,data)
        elif ch==2:
                name=input("Enter file name: ")
                read_from(name)

for i in range(10):
        ask_user()
        

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger Shack" Per the client, you have the following information: the client’s name, burger’s name, time of the day, and the total bill. By the end of the day, your program will provide the following information: 1. Top three best clients (highest bills) 2. Name of the client with the second-to-last lowest bill 3. Busiest hour of the day (number of clients) Assumptions: 1. doesn't...
Do it in python please Write a program using functions and mainline logic which prompts the...
Do it in python please Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a...
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
Write a python program that has subprograms (functions) nested 3 deep. Each subprogram should reference variables...
Write a python program that has subprograms (functions) nested 3 deep. Each subprogram should reference variables defined in each of its enclosing subprograms. To submit this program, copy/paste the program and a screenshot of your output when you run it. Use comments liberally so that we can understand your work! You can run JavaScript wherever you prefer, but it is available on repl.it.
Write a Python program with correct indentation using functions and mainline logic which prompts the user...
Write a Python program with correct indentation using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum,...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT