Python代写:CIS41Scores


代写Python基础作业,从文件中读取Scores数据并展示。

Requirement

Write a Scores class that reads data from a file of scores and prints some
aspects of the data.

Input file

The input file, input1.txt, contains scores of several countries in a study.
The scores are in 6 columns for 6 countries, but your code should not use the
constant 6.
The first line of the file has 6 country name abbreviations, each subsequent
line has 6 numbers for each of the 6 countries. The 6 columns are separated by
a tab, but your code should work with any whitespace delimiter.

Driver code

Start with the file assignment1.py, which has the main driver code to work
with a Scores object that you will write. These are 7 lines of code in the
main function:

  • create a Scores object
  • set up a generator to get the scores of one country at a time
  • use the generator to get and print the first country’s scores
  • print all scores of all countries, sorted by the last scores of each country
  • use the generator to get and print the next country’s scores
  • out of all the total scores of each country, print the minimum and maximum total scores
  • use the generator to get and print the next country’s scores

Scores class

Write a Scores class in a file called scores.py that has the following
methods:

  • a constructor that reads in data from the file and stores it in an appropriate data structure. The data structure should be an instance variable. Your code should check for file open success and end the program with an error message if the file open fails. +2pts Extra Credit if your code doesn’t use any loop (comprehension is okay)
  • a method, getOne(), that serves as a generator for the data. It will return one country’s name and corresponding scores with each next() call. The order of the country being returned is alphabetical order based on country abbreviation.
  • a method, printByLast(), that prints all countries and all corresponding scores. Each country and its scores are on one line of output, and the print order is by descending order of the last score of each country (see program output).
  • a method, printMaxMin, that prints the maximum and minimum of all the total scores of each country. The total score of each country is the sum of all the scores that belong to the country. +1pt Extra Credit if your code doesn’t use any loop (comprehension is okay)
    Don’t forget a docstring for each method. It is required for all methods that
    you write in an assignment.
    The docstring can be as simple as a one-line description of what the method
    does: ‘’’ print max and min of total scores ‘’’ or you can also add
    descriptions for input data and return data.
    [For the 4 methods listed above, the numbers of lines of code I have for each
    function body are 7, 2, 2, 3. (And the code used ‘normal’ spacing, i.e. for
    loop header and for loop body are on 2 lines, not jammed into one).
    Python can help us do a lot of work in relatively few lines of code. This is
    good for quick prototyping of ideas / algorithms, and is especially good for
    Adv Python students who only have to write 14 lines of code for an
    assignment!]

Program output

1. ('COL', ['15', '14', '8.8', '13.5', '14', '12.6', '11.5', '11.2', '19.6'])
2. MEX: 15 14 15 11.6 15 14.5 8.2 12.5 24.5
3. ZWE: 15 14.5 8 13 14.5 12 11.6 4.5 24.5
4. NZL: 15 14 13 14.5 15 13 12.5 11.5 24
5. JPN: 15 13 13 14 15 14 14 13 22.5
6. UKR: 15 15 15 15 16 12.5 12 8 20.5
7. COL: 15 14 8.8 13.5 14 12.6 11.5 11.2 19.6
8. ('JPN', ['15', '13', '13', '14', '15', '14', '14', '13', '22.5'])
9. max total 133.5
10. min total 117.6
11. ('MEX', ['15', '14', '15', '11.6', '15', '14.5', '8.2', '12.5', '24.5'])

The line numbering is only for the explanation below, it is not part of the
program output.

  • Lines 1, 8, 11 are the result of printing by using the generator. Note that the country abbreviations are in alphabetical order.
  • Lines 2-7 are the result of printing all data, note the descending order of the last scores.
  • Lines 9-10 are the maximum and minimum of the total scores
    When done testing, make sure you have a first documentation block for
    scores.py.
    Fill in your name and the correct lab number. This documentation block is
    required for all assignments that you turn in.
    Then upload your scores.py file and keep it the same filename (not
    scores_final.py or scoresLastNameFirstName.py)

文章作者: SafePoker
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 SafePoker !
  目录