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...
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...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT