Question

NOTE: USE PYTHON CS160 Computer Science Lab 14 Working with lists, functions, and files Objective: Work...

NOTE: USE PYTHON

CS160 Computer Science Lab 14

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’tnot 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.

A sample session of this part might be:

Enter the scores, press enter to quit:
Score: 100 79 67 32 -1 - these will all be on separate lines
<>
Create another file? yes
Enter the scores, press enter to quit:
Score: 67 200 50 -1 – again, one number per line
<>
Create another file? no

After the above session, the contents of first file would be:

100 79 67 32

And the contents of the second file would be:

67 200 50

Make sure to create two or more files.

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.

A sample session of the part might be (using the sample data from above):

<>
 Largest score:     100
 Smallest score:     32
 Average score:   69.50
 Searching for values
 Enter the lower limit: 60
 Enter the upper limit: 80
 Matching values:
 79
 67

These values are calculated-in and returned-from functions. The values are printed in main.

These values are asked for in main and passed as arguments to the “matching values” function.

This information will be determined and printed inthe “matching values” function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#########################################PART A #########################################

def get_test_scores():
    score_list=[]
    print('Enter the scores, press -1 to quit:')
    while True:
        score =int(input('Enter score: '))
        if score<0:break
        score_list.append(score)
    return score_list

def create_score_file(score_list,filename):
    with open(filename,'w') as outfile:
        for score in score_list:
            outfile.write(str(score)+'\n')

def main():
    file_number=1
    while True:
        scores = get_test_scores()
        create_score_file(scores,'score_'+str(file_number)+'.txt')
        file_number+=1
        another_file=input('Create another file? ')
        if another_file=='yes':continue
        else:break
main()


#########################################PART B #########################################
import os

def read_file_scores(filename):
    if os.path.isfile(filename) is not True:return None
    with open(filename,'r') as infile:
        return [int(score.strip()) for score in infile.readlines()]

def largest_score(scores):
    return  max(scores)

def smallest_score(scores):
    return  min(scores)

def average_score(scores):
    return sum(scores)/float(len(scores))


def print_matching_values(scores_list, lower_limit, upper_limit):
    print('Matchin values: ')
    for score in scores_list:
        if lower_limit<=score and score<=upper_limit:
            print(score)


def main():
    filename=input('Enter valid file name: ')
    scores_list=read_file_scores(filename)
    if scores_list is None:
        print('Invalid file entered. Program will terminate')
        return
    print('Largest score  :    {}'.format(largest_score(scores_list)))
    print('Smallest score :    {}'.format(smallest_score(scores_list)))
    print('Average score  :    {0:.2f}'.format(average_score(scores_list)))
    lower_limit=int(input('Enter the lower limit: '))
    upper_limit = int(input('Enter the upper limit: '))
    print_matching_values(scores_list,lower_limit,upper_limit)


main()

################################################################################################
Add a comment
Know the answer?
Add Answer to:
NOTE: USE PYTHON CS160 Computer Science Lab 14 Working with lists, functions, and files Objective: Work...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in te...

    Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str-- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents...

  • Create a java project with the name Assignment3YourLastName in the folder COMP3110AssignmentsYourLastName Part-1 (use a nested-if-else...

    Create a java project with the name Assignment3YourLastName in the folder COMP3110AssignmentsYourLastName Part-1 (use a nested-if-else statement) Add a static method with the name gradeLetter. This method will ask the user to enter an integer score and display the grade letter based on the rules: Score range Grade letter 90—100 A 80—89 B 70—79 C 60—69 D 0—59 F Part-2 (use a while statement) Add a static method with the name CalculateGrade. This method will ask the user to enter...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • You will be writing some methods that will give you some practice working with Lists. Create...

    You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class named List Practice in the project. Then paste in the following code: A program that prompts the user for the file names of two different lists, reads Strings from two files into Lists, and prints the contents of those lists to the console. * @author YOUR NAME HERE • @version DATE HERE import java.util.ArrayList; import java.util.List; import...

  • Please help with this exercises in c++ language Thanks Lab Activity #5-More Functions/FILES Exercise# Write a...

    Please help with this exercises in c++ language Thanks Lab Activity #5-More Functions/FILES Exercise# Write a program that will generate 1000 random numbers (all of which are between 10 and 20) and then write all of the even numbers (from those 1000 numbers) to a new file called "myEvenRandoms.txt Exercise #2 (DO ONLY IF WE COVERED ARRAYS OF CHARACT Write a program that will ask the user to enter a sentence. Then create a separate function that will count the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT