Question

In: Computer Science

PYTHON...... Working with lists, functions, and files Objective: Work with lists Work with lists in functions...

PYTHON......

Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment:

Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1.

After the scores have been entered, ask the user to enter a file name. If the user doesn’t not enter a file name exit the program. If the user does enter a file name, write out the scores to the specified file. Then ask the user if they wish to create another data file. Continue on with this process until the user indicated they do not wish to create another data file or they do not enter a file name.

Part 2: Write a program that will ask the user for a data file. Use one of the data files created from part 1. Each line of the file must contain a single value. Fill a list with the values from the file.

Once the list has been filled with values, the program should display, with appropriate labels, the largest value from the list, the smallest value from the list, and the average of the list (with 2 places after the decimal point). Then ask the user for a lower limit and an upper limit. Display all the values from the list that fall within that range.

The code to find the largest, smallest, and average must be done in value-returning functions. Do not use any built-in functions to find these values, write loops to implement these functions. The code to print out a range of scores must be done a function. The arguments for that function MUST be a list, a lower limit and an upper limit. The “matching values” function can print out values. For the rest of the program, only the main function will ask questions or print output.

Solutions

Expert Solution

createdatafiles.py: (part - 1)

Code:

if __name__ == '__main__':
    while True:
        values = []
        print("Enter Data values: ")

        # read data values upto -1
        while True:
            val = int(input())
            if val == -1:
                break
            values.append(val)

        print("Enter file name: ", end='')
        file_name = input()  # read file name

        # if file name is not entered break
        if len(file_name) == 0:
            break

        # if extension is not mentioned in the file name
        if not file_name.endswith('.txt'):
            file_name += '.txt'

        # creates and opens file and write values
        # if file already exists it overrides
        file = open(file_name, 'w+')
        for val in values:
            file.write(str(val) + '\n')
        file.close()

        # if the user wants to enter new files
        print("Enter new data values?(y/n): ", end='')
        option = input()
        if option.lower() == 'n':
            break

Screenshots of code for indentation:

Sample output for part 1:

readdatafiles.py: (part - 2)

Code:

def largest(values):
    max_val = values[0]
    for val in values:
        if val > max_val:
            max_val = val

    return max_val


def smallest(values):
    min_val = values[0]
    for val in values:
        if val < min_val:
            min_val = val

    return min_val


def average(values):
    sum = 0
    for val in values:
        sum += val

    return sum / len(values)


# returns list of values between limits
def matching(values, upper, lower):
    match = []
    for val in values:
        if lower <= val <= upper:
            match.append(val)

    return match


if __name__ == '__main__':
    while True:
        print("Enter file name to perform analysis: ", end='')
        file_name = input()

        # if extension if not mentioned
        if not file_name.endswith('.txt'):
            file_name += '.txt'

        # check if file exists
        try:
            file = open(file_name, 'r')
        except FileNotFoundError:
            print("File not found")
            continue

        # read content and convert them into list of ints
        content = file.readlines()
        values = [int(line.strip()) for line in content]

        file.close()

        print("Largest value: ", largest(values))
        print("Smallest value: ", smallest(values))
        print("Average value: {0:.2f}".format(average(values)))

        print("Enter upper limit: ", end='')
        upper = int(input())

        print("Enter lower limit: ", end='')
        lower = int(input())

        # if a = [1, 2, 3], print(*a) => 1 2 3
        print("Matching values: ", *matching(values, upper, lower))

        print("Analyze new file?(y/n): ", end='')
        option = input()
        if option.lower() == 'n':
            break

Screenshots of Code for indentation:

Sample output of part - 2:

Please rate the answer if you like it.


Related Solutions

PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX file system. Procedure: 1. OpenyourUnixshellandtrythesecommands: Ø Create a new file and add some text in it vcat > filename Ø View a file vcat /etc/passwd vmore /etc/passwd vmore filename Ø Copy file, making file2 vcp file1 file2 Ø Move/rename file1 as file2 vmv file1 file2 Ø Delete file1 as file2 vrm file //Deletefile //Double-checkfirst vrm -i file Ø Counts the lines, words, characters in...
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
Objective Work with strings Work with files Work with formatted output This program will display a...
Objective Work with strings Work with files Work with formatted output This program will display a very basic handling of transactions for a checking account. The program will read in a data file with all the past transactions. Use FileUtil.selectOpenFile or input() to get a file name from the user. The format of the file will be very basic, just a date (as a string in the format of MM/DD), a single letter which indicates the type of transaction (‘B’...
2. working with databases and files in python a) Write a function with prototype “def profound():”...
2. working with databases and files in python a) Write a function with prototype “def profound():” that will prompt the user to type something profound. It will then record the date and time using the “datetime” module and then append the date, time and profound line to a file called “profound.txt”. Do only one line per function call. Use a single write and f-string such that the file contents look like: 2020-10-27 11:20:22 -- Has eighteen letters does 2020-10-27 11:20:36...
Overview: You will write a python program that will work with lists and their methods allowing...
Overview: You will write a python program that will work with lists and their methods allowing you to manipulate the data in the list. Additionally you will get to work with randomization. Instructions: Please carefully read the Instructions and Grading Criteria. Write a python program that will use looping constructs as noted in the assignment requirements. Name your program yourlastname_asgn5.py Assignment Requirements IMPORTANT: Follow the instructions below explicitly. Your program must be structured as I describe below. Write a Python...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the...
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the question above in a few sentences.
"Python lists are power data structures. Describe some of the benefits of Python lists" Make sure...
"Python lists are power data structures. Describe some of the benefits of Python lists" Make sure you don't just give me a list but a few sentences instead just answering it.
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience on the use of: 1. integers (int) 2. floats (float) 3. conditionals(if statements) 4. iteration(loops) The goal of this project is to make a fictitious comparison of the federal income. You will ask the user to input their taxable income. Use the income brackets given below to calculate the new and old income tax. For the sake of simplicity of the project we will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT