首页 > > 详细

辅导留学生Python设计、辅导留学生Sale Price Table设计

This assignment requires three short programs. For all three programs:
• use meaningful names for variables and functions.
• add any comments you think will help explain what your program is
doing
Part 1: Sale Price Table
Write a program that calculates the sale prices for items normally priced
from $9.95 to $49.95, where the sale price can be 5% to 50% off. Display
the results in an appropriately formatted table. An example of the layout
formatting is shown below
Normal p r i c e : $9 . 9 5 $14 . 9 5 $19 . 9 5 $24 . 9 5 $29 . 9 5 $34 . 9 5 $39 . 9 5 $44 . 9 5 $49 . 9 5
%o f f : 5% 9 . 4 5 14.20 18.95 23.70 28.45 33.20 37.95 42.70 47.45
%o f f : 10% 8 . 9 6 13.46 17.95 22.45 26.95 31.46 35.96 40.46 44.96
%o f f : 15% 8 . 4 6 12.71 16.96 21.21 25.46 29.71 33.96 38.21 42.46
%o f f : 20% 7 . 9 6 11.96 15.96 19.96 23.96 27.96 31.96 35.96 39.96
%o f f : 25% 7 . 4 6 11.21 14.96 18.71 22.46 26.21 29.96 33.71 37.46
%o f f : 30% 6 . 9 6 10.46 13.96 17.46 20.96 24.46 27.96 31.46 34.96
%o f f : 35% 6 . 4 7 9 . 7 2 12.97 16.22 19.47 22.72 25.97 29.22 32.47
%o f f : 40% 5 . 9 7 8 . 9 7 11.97 14.97 17.97 20.97 23.97 26.97 29.97
%o f f : 45% 5 . 4 7 8 . 2 2 10.97 13.72 16.47 19.22 21.97 24.72 27.47
%o f f : 50% 4 . 9 7 7 . 4 7 9 . 9 7 12.47 14.97 17.48 19.98 22.48 24.98
You must use nested for loops, one inside the other. Solutions creating the
results by adding lines like:
print(" 5% 9.45 14.20 18.95 23.70 28.45 33.20 37.95 42.70 47.45")
print("10% 8.95 13.46 17.95 22.45 26.95 31.46 35.96 40.46 44.95")
will be awarded zero marks.
Full marks will be awarded to those programs that produce output like that
above with the decimal points all lined up, and the $ sign on the top line
but not the others.
Marks will also be awarded for good program design. In particular, your
code should not include “magic numbers”.
[4 marks]
Part 2: A Movie Title Explorer
There is a file of the top 1000 or so movie titles on Stream calledmovies.txt.
For this question you are to build a way of displaying a random movie title,
finding movies, and building and saving a list of movies you would like to
watch.
The program first displays a menu (something like that shown below) and
carries out the appropriate action depending on which letter the user types,
and then redisplays the menu:
*** Movie Title Explorer ***
l - load file of movie titles
r - random movie
s - search
sw - starts with
k - keep: save the last displayed movie title to your favourites
f - favourites display
c - clear favourites
q - quit
command: ?
load Load a file of movie titles. Prompt for the name of a file. If no name is
input (e.g. if using input() and the user presses the enter key), load
from movies.txt, otherwise load it from the specified filename.
The file of movie titles is a text file is formatted with one movie per
line.
random Randomly pick a movie title and display it.
search Prompt for a string, and then search for and display all movies
that contain this string. Search case-independently, and separate the
movies with a blank line. If the search string is empty, exit the search
and redisplay the menu.
starts with Prompt for a string and then search for and display all movies
that start with this string. Search case-independently, and separate
movies with a blank line. If the search string is empty, exit the search
and redisplay the menu.
keep Add the last displayed movie to a list of your favourites.
favourites Display the current favourites list.
clear Clear the favourites list.
quit Quit the program.
Don’t forget to add appropriate diagnostics. E.g. you can’t display a ran-
dom or search before the movies file has been loaded.
IMPORTANT: for full marks, make sure that you use functions, one for
each of the commands. You can of course, create any additional functions
that will help to simplify or clarify your code.
Some suggestions on getting started:
1. Get the menu going by using a while loop. Inside the while loop
display the menu and ask the user to input a command. Then use
if/elif statements for each command. Initially, you can put a print
statement inside each if statement—later you will call the function for
that particular command.
2. Create a function for each command e.g. load, search,. . . . Initially,
simply put a print statement inside each function, e.g. for the keep
function, put print("KEEP"). You can test each command—it will
simply print out a message.
3. Then get load going so you can load a text file into a list. We haven’t
yet covered file inpu/output, so you can just cut and paste the fol-
lowing function to read the movies from a file and return a list of
movies.
def read_file(filename):
file = open(filename, "r") # open the file
movies_list = file.readlines() # load each line into a list
file.close() # close the file
# Now get rid of the "\n" at the end of each line using
# a "list comprehension". This is a construct we haven't
# yet covered, so for now just use the code. We'll explain
# later.
movies_list = [movie.strip() for movie in movies_list]
return movies_list
4. Implement the random command next. If you import the random
module, you will find the random.choice() method useful here. It
returns a randomly chosen element from a list. e.g.
print(random.choice([1, 2, 3, 5, 9]))
5. Then get quit going, search, startswith search, and after that the
favourites related commands.
[6 marks]
Part 3
There is a link on stream called stories.txt. Copy and paste code into
your program. These are the data you’re going to use to do the following.
It consists of a list of lists, where each sub-list contains two elements, an
ultrashort story and its author.
Create a program that lets you:
• Find stories that contain a certain pattern (e.g. sky)
• Find all stories by a certain author
• Find stories that are by a certain author and contain a certain word
• Find stories less than a certain number of words, using the .split()
method to (roughly) split a line into words. You can ignore punctua-
tion when doing this. If there are no stories, display a message stating
this.
• Display all the stories
In the above, that matching is based on substring (i.e. pattern in line), so
search for “wood” would match “Atwood”. Exact author searches aren’t
necessary. The same goes for finding stories that contain a certain work.
When displaying the stories, format them with quotes around the story and
the authors name on a following line, indented, like this:
"The baby's blood type? Human, mostly."
-- Orson Scott Card
Follow the model of using a menu outlined in the previous question.
[5 marks]
Submission
ALL submissions must be via Stream (not email) using the Assignment 2A
submission link.
Submit your Python programs, each named with.pyextensions. You should
have three .py files to submit, one for each part (eg part1.py, part2.py,
and part3.py)
Do not submit Word documents (.doc or .docx) or .zip files.
Check that your files have a .py extension and that all programs display
your name and ID number when starting

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!