辅导案例-CMPT 120-Assignment 4

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
1








Simon Fraser University




CMPT 120: Assignment 4







The objective of this assignment is to improve the “travel agent” bot from
Assignment 1.



Problem Description
In this assignment we want to improve the “travel agent bot” from
Assignment 1. The goal is to easily extend the bot to incorporate new
destinations (in addition to Vienna and Bali) and additional “highlights” (in
addition to “classical music” and “beaches and surfing”) by means of using
files and representing them as lists. Also, the program’s structure ought to be
improved through the usage of functions, and user inputs ought to be
validated.
As before, the bot will first display a welcome message. Then it will
ask for the user’s name and age. If the user is 65 or older, it will inform
them about a senior discount. Next, it asks how long (how many nights)
2




the trip is going to take. Then it interviews the user about whether they
like or dislike the various “highlights” that the destinations may or may not
have. Based on the user’s replies, it will suggest and display the costs of
one trip the travel agency currently offers or say that they don’t have any
offers matching the user’s preferences.
As before, the bot will then ask the user whether they are interested
(even if no offer was shown). If that is the case, the bot will provide a one-
time password for the user to enter on the travel agency website. If the user
is not interested, the bot will just say goodbye. Then the program will end.

Stage 1: Restructure the Previous Chatbot
This assignment will work in two stages. In the first stage, the chatbot
from Assignment 1 is to be restructured to make use of functions, but
without changing the program’s behavior. For this part, you may
use the sample solution provided for Assignment 1 in Canvas. Rework the
program to define and call the following functions:
1. welcome:
Purpose: Displays the welcome message.
Arguments: none
Returns: nothing
2. ask user information:
Purpose: Asks the user for their name, age, and how many nights
they want to stay.
Arguments: none
Returns: the user’s name (a string), age (an integer), and the number
of nights (an integer)
3. ask user preferences:
Purpose: Asks the user whether they like classical music, and whether
they like beaches and surfing.
Arguments: none
Returns: whether the user likes classical music (a Boolean), and
whether the user likes beaches and surfing (a Boolean)
4. ask yes no:
3




Purpose: Displays a question to the user and prompt them to type
in either “y” or “n”. (Here, it won’t be necessary to cover all
variations of “yeses” and “noses”. Any response other than “y”
and “n” can be interpreted as “no”.)
Arguments: the question (a string)
Returns: whether the user responded positively or negatively (a Boolean)
5. ask number:
Purpose: Displays a question to the user and prompt them to type
in a number.
Arguments: the question (a string)
Returns: a number typed in by the user (an integer)
6. compute discount percentage:
Purpose: Determine discount in terms of percentage points the user
gets for their age.
Arguments: age (an integer)
Returns: the discount percentage (an integer)
7. compute total cost:
Purpose: Computes the total cost of a trip.
Arguments: the price for the flight (a float), the price for the hotel (a
float), the number of nights (an integer), and an age (an integer)
Returns: the total cost (a float)
8. show trip details:
Purpose: Displays the information for a trip.
Arguments: the destination (a string), the price for the flight (a
float), the price for the hotel (a float), the number of nights (an
integer), the user’s age (an integer)
Returns: nothing
9. suggest trip:
Purpose: Chooses one destination to suggest to the user.
Arguments: whether the user likes classical music (a Boolean), whether
the user likes beaches (a Boolean), the cost of a trip to Vieanna
(a float), and the cost of a trip to Bali (a float)
4




Returns: the string ”Vienna”, or the string ”Bali”, or the value None,
according to the rules for suggesting a trip from Assignment 1:
If the user prefers culture over beaches, the bot will suggest the
trip to Vienna. In case they prefer beaches over culture, they
will suggest the trip to Bali. If they like both, the bot will try to
maximize the travel agency’s profit by recommending whatever
trip has the highest total costs (if both cost the same, Bali is
chosen).
10. ask to create account:
Purpose: Ask the user whether to create an account. If they answer
positively, display the one-time password.
Arguments: none
Returns: nothing

Restructuring the program should be an iterative process. This means
that you should always only change a small piece of code, say by defining
a single new function, and then run the program and test whether it still
works as before, for example by following the provided sample runs. Only
once you have eliminated all new errors that perhaps occur, move on to
change the next thing.
When implementing a new function, also make sure that you identify
the various parts of the program from which the function is to be called.
Remember that it is possible for one function to call another function. If
some piece of code is identical or very similar to the body of a function
body, then this strongly indicates that instead the function should be called
at that point. For example, notice that the functions ask user preferences
and ask to create account involve asking the user yes/no questions, which
should be done by means of calling the ask yes no function.

Sample Runs for Stage 1
What the user types is underlined and highlighted. The sample runs below
are essentially the same as for Assignment 1.1
============ Sample run 1.1 ============

Welcome ! I am your friendly travel agent bot .

1The only changes are that since “culture” is a very broad term, the feature “culture
and classical music” has been renamed to “classical music”, and that prices are rounded,
so as to display 1860.97$ instead of 1860.9699999999998$.
5




I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Alice
Hello dear Alice !

What is your age ? --> 23

For how many nights do you want stay ? --> 6
Do you like classical music ?
Please answer y/ n. --> y
Do you like beaches and surfing ?
Please answer y/ n. --> n


How about a trip to Vienna ?
Flight : 997 . 93 $
Hotel : 143 . 84 $/ night
Discount : 0%
Total for 6 nights ( incl . discounts ): 1860 . 97 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> y

Looking forward to working with you !
Your one - time password is: eeeeeee A

Goodbye .

============ Sample run 1.2 ============

Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .
6





What is your name ? --> Bob
Hello dear Bob !

What is your age ? --> 71
Great ! We offer a senior discount .
For every year over 64 , you get 1% off .

For how many nights do you want stay ? --> 10
Do you like classical music ?
Please answer y/ n. --> y
Do you like beaches and surfing ?
Please answer y/ n. --> y


How about a trip to Bali ?
Flight : 849 . 93 $
Hotel : 235 . 35 $/ night
Discount : 7%
Total for 10 nights ( incl . discounts ): 2979 . 19 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> n

Sorry to hear that . Please consider using our service again .
Goodbye .
============ Sample run 1.3 ============

Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Cathleen
Hello dear Cathleen !

What is your age ? --> 42
7






For how many nights do you want stay ? --> 7
Do you like classical music ?
Please answer y/ n. --> y
Do you like beaches and surfing ?
Please answer y/ n. --> y


How about a trip to Bali ?
Flight : 849 . 93 $
Hotel : 235 . 35 $/ night
Discount : 0%
Total for 7 nights ( incl . discounts ): 2497 . 38 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> y

Looking forward to working with you !
Your one - time password is: nnC !!!!

Goodbye .
============ Sample run 1.4 ============

Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Doug
Hello dear Doug !

What is your age ? --> 99
Great ! We offer a senior discount .
For every year over 64 , you get 1% off .

For how many nights do you want stay ? --> 5
Do you like classical music ?
Please answer y/ n. --> n
8





Do you like beaches and surfing ?
Please answer y/ n. --> n


I Im sorry , we don It have any trips to offer at this point .

Are you interested , and want to create a user account ?
Please answer y/ n. --> n

Sorry to hear that . Please consider using our service again .
Goodbye .
Stage 2: Include More Destinations and Highlights
In the second stage, new destinations and highlights are to be incorporated
by means lists, to be read from files. Create a copy of your solution to stage
1, and revise it as follows.

Destinations
Now, the travel agency offers multiple trips, for example with the following
prices:

Destination Flight Hotel (per night)
Vienna $997.93 $143.84
Bali $849.93 $235.35
Las Vegas $338.37 $173.54
Macao $734.34 $199.99
To represent this data in the program, use a list destinations that holds
the names of the destinations (as strings) as well as lists flight prices
and hotel prices of the same length that hold the corresponding prices
for flights and hotels, respectively. Note: When grading the assignment,
different values from the ones provided here might be used. Your solution
should still work when these lists are changed or extended. To receive bonus
points, read this data from the .csv files provided with this assignment
instead of hard-coding them (see below).
Since the three lists are of the same length and follow the same order,
this will allow us to access the flight or hotel price of a destination by
means of their index, i.e. their position in the list. For example, when Las
Vegas is at the third position and thus has index 2 in list destinations,
9




flight prices[2] will give us the price for the flight to Las Vegas. Revise
the following functions accordingly:
• compute total cost:
Purpose: Computes the total cost of a trip.
Arguments: the destination (a string), the number of nights (an
integer), and an age (an integer)
Returns: the total cost (a float)
• show trip details:
Purpose: Displays the information for a trip.
Arguments: the destination (a string), the number of nights (an
integer), the user’s age (an integer)
Returns: nothing

Highlights
The travel agency now distinguishes several highlights, for example “classical
music”, “beaches and surfing”, “history and architecture”, “gambling”, and
“musicals”. Use a list called highlights that contains such strings. Again,
your solution should still work when different values are used, and for bonus
points have your program read them from the corresponding .csv file.
Then revise the function ask user preferences to use a for loop to
iterate over the highlights in this list, asking the user whether they like
them in each case. The function should now return a list of Boolean values,
one for each highlight.

Matching Highlights
The following table shows an example of which of the destinations offers
which of the highlight features:

classical
music
beaches and
surfing
history and
architecture
gambling musicals
Vienna ✓ ✓
Bali ✓ ✓
Las Vegas ✓ ✓
Macao ✓ ✓
To represent this information, use a list dest_highlights that contains for
each destination a list of Boolean values indicating whether the destination
10




offers the corresponding highlights. dest_highlights is hence a list of lists.
The outer list has the same length as list destinations (4), and each of
its elements is itself a list of the same length as highlights (5). Again,
ensure that your solution works for different values, and for bonus points
make your program read them from a file.
For matching destinations to user preferences, revise the function suggest trip
as follows:
• suggest trip:
Purpose: Determine which destination which “matches” best to the
user preferences, i.e., the number of positions where both lists
contain True.
Arguments: a list of Boolean values, representing the user
preferences
Returns: a string, representing a destination
The bot ought to suggest whatever destination offers the most features that
the user prefers. In the above example, if the user likes “beaches and
surfing”, “history and architecture”, and “musicals” (and none of the
others), it will suggest Bali, as it matches 2 preferences. In the program,
this means that the destination whose list in dest_highlights has the
most True values at the same positions as the list representing the user’s
preferences is the best match (False values at the same position don’t
count).
When there are multiple destinations with the same, maximal match
count, the one that comes first in list destinations is chosen. To keep
things simple, we don’t “tie-break” by comparing the total costs anymore.
Only if the number of matches comes out zero, which can only happen when
the user said “no” to each one of the highlights, the bot will respond saying
“I’m sorry, we don’t have any trips to offer at this point” (see sample run
2.3).
Note that for this part of the program, you will have to make use of
a list of accumulators, each element being the number of matches for one
of the destinations. Computing the matches will require nested loops. To
determine the maximal match count from the list of accumulators, you can
use the built-in function max, but note that you want to determine the
position (representing the destination) with the maximal match count, not
the maximal number of matches itself.
11




Using Files (Bonus)
Reading Data from Files
The assignment description in Canvas contains the .csv files (CSV stands
for “comma separated values”)

destinations.csv
highlights.csv
flight prices.csv
hotel prices.csv
dest highlights.csv
Make your program read its data from these files for bonus points.
You are encouraged to use the interactive textbook as reference to see
more details about reading and writing data files, Chapter 11. Still, Python
code for both reading and writing data from/to files is provided with this
assignment.
The function read string list from file(...) takes a string as
argument, containing a file name (it is assumed that the file is located in the
same folder as your Python program), and returns a list of strings, each of
which contains the content of one line of the file being read, in the same
order (without the newline character ’\n’ at the end). For example, the call
read string list from file("highlights.csv")
will return the list ["classical music", "beaches and surfing", "history
and architecture", "gambling", "musicals"]. You may develop a
different function to read the data. You may incorporate this function verbatim
in your code, including the comments. If you revise the function, clarify how
you do it.
Note that for destinations.csv and highlights.csv, calling the
function is enough. For flight prices.csv and hotel prices.csv, the
strings have to be converted into floats afterwards. To get the list of lists
dest_highlights from the file dest_highlights.csv, each string
has to be converted into a list of Booleans, for which you first need to split the
string, e.g. using the built-in string method .split(...).

Writing Data to Files
After the dialog with the user, your program should write the user-specific
data (i.e., their name, age, and preferences) to the file users.csv. Here
you can use the function append line to file(...) that is provided. It
12

takes two string arguments: the first one is a single string that contains
all data to be written, the second one is the name of the file to be written
to (again it is assumed that this file is the same folder as your program).
The function will append the string as a new line at the end of the file,
leaving all previous file contents intact. Running your program multiple
times hence will successively add more and more lines to the file, one for
each new user. Check the requirements and assumptions in the function
code and comments.
Use a similar representation for this output data as is used for the input
data. That is to say, the different values are separated by commas, and
Boolean values are represented by ”1” and ”0”. After the five sample runs
shown below, the content of the file users.csv should be:
Alice,23,1,0,1,0,0
Bob,71,0,1,1,0,1
Cathleen,42,0,0,0,0,0
Doug,99,0,0,1,1,0
Erica,27,1,0,1,0,1

User Input Validation
Revise the function ask yes no such that they validate the user input, and
prompt them to type in a new answer until they either types "y" or "n".
Similarly, revise function ask number to prompt the user to type in a new
answer as long as their input is not a sequence of digits. See sample run 2.5
below. (Hint: You may want to use the string method isdigit)

Sample Runs for Stage 2
The below sample runs incorporate trace messages with the purpose of
illustrating how matching is supposed to work. You do not need to include
them in your submission. (Of course, you are free to make use of trace
message while working on your solution.)
============ Sample run 2.1 ============

Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
13





with a one - time password to create a user
account on our website .

What is your name ? --> Alice
Hello dear Alice !

What is your age ? --> 23

For how many nights do you want to stay ? --> 6

Do you like classical music ?
Please answer y/ n. --> y
Do you like beaches and surfing ?
Please answer y/ n. --> n
Do you like history and architecture ?
Please answer y/ n. --> y
Do you like gambling ?
Please answer y/ n. --> n
Do you like musicals ?
Please answer y/ n. --> n

** Trace : Vienna matches preference for classical music
** Trace : Vienna matches preference for history and architecture
** Trace : matches for destination Vienna : 2

** Trace : Bali matches preference for history and architecture
** Trace : matches for destination Bali : 1

** Trace : matches for destination Las Vegas : 0

** Trace : Macao matches preference for history and architecture
** Trace : matches for destination Macao : 1

How about a trip to Vienna ?
Flight : 997 . 93 $
Hotel : 143 . 84 $/ night
Discount : 0%
Total for 6 nights ( incl . discounts ): 1860 . 97 $
14





Are you interested , and want to create a user account ?
Please answer y/ n. --> y

Looking forward to working with you !
Your one - time password is: eeeeeee A !!

Goodbye .

============ Sample run 2.2 ============

Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Bob
Hello dear Bob !

What is your age ? --> 71
Great ! We offer a senior discount .
For every year over 64 , you get 1% off .

For how many nights do you want to stay ? --> 10

Do you like classical music ?
Please answer y/ n. --> n

Do you like beaches and surfing ?
Please answer y/ n. --> y

Do you like history and architecture ?
Please answer y/ n. --> y

Do you like gambling ?
Please answer y/ n. --> n

Do you like musicals ?
Please answer y/ n. --> y


** Trace : Vienna matches preference for history and architecture
15





** Trace : matches for destination Vienna : 1

** Trace : Bali matches preference for beaches and surfing
** Trace : Bali matches preference for history and architecture
** Trace : matches for destination Bali : 2

** Trace : Las Vegas matches preference for musicals
** Trace : matches for destination Las Vegas : 1

** Trace : Macao matches preference for history and architecture
** Trace : matches for destination Macao : 1

How about a trip to Bali ?
Flight : 849 . 93 $
Hotel : 235 . 35 $/ night
Discount : 7%
Total for 10 nights ( incl . discounts ): 2979 . 19 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> n

Sorry to hear that . Please consider using our service again .
Goodbye .
============ Sample run 2.3 ============
Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Cathleen
Hello dear Cathleen !

What is your age ? --> 42

For how many nights do you want to stay ? --> 7
16





Do you like classical music ?
Please answer y/ n. --> n
Do you like beaches and surfing ?
Please answer y/ n. --> n
Do you like history and architecture ?
Please answer y/ n. --> n
Do you like gambling ?
Please answer y/ n. --> n
Do you like musicals ?
Please answer y/ n. --> n


** Trace : matches for destination Vienna : 0
** Trace : matches for destination Bali : 0
** Trace : matches for destination Las Vegas : 0
** Trace : matches for destination Macao : 0
I Im sorry , we don It have any trips to offer at this point .
Are you interested , and want to create a user account ?
Please answer y/ n. --> y
Looking forward to working with you !
Your one - time password is: nnC !

Goodbye .
============ Sample run 2.4 ============
Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Doug
Hello dear Doug !
17





What is your age ? --> 99
Great ! We offer a senior discount .
For every year over 64 , you get 1% off .

For how many nights do you want to stay ? --> 5

Do you like classical music ?
Please answer y/ n. --> n
Do you like beaches and surfing ?
Please answer y/ n. --> n
Do you like history and architecture ?
Please answer y/ n. --> y
Do you like gambling ?
Please answer y/ n. --> y
Do you like musicals ?
Please answer y/ n. --> n

** Trace : Vienna matches preference for history and architecture
** Trace : matches for destination Vienna : 1

** Trace : Bali matches preference for history and architecture
** Trace : matches for destination Bali : 1

** Trace : Las Vegas matches preference for gambling
** Trace : matches for destination Las Vegas : 1

** Trace : Macao matches preference for history and architecture
** Trace : Macao matches preference for gambling
** Trace : matches for destination Macao : 2

How about a trip to Macao ?
Flight : 734 . 34 $
Hotel : 199 . 99 $/ night
Discount : 35%
Total for 5 nights ( incl . discounts ): 1127 . 29 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> n
18





Sorry to hear that . Please consider using our service again .

Goodbye .

============ Sample run 2.5 ============
Welcome ! I am your friendly travel agent bot .
I will ask you some questions , and make a
recommendation based on your answer .
If you are interested , I will provide you
with a one - time password to create a user
account on our website .

What is your name ? --> Erica
Hello dear Erica !

What is your age ? --> 27.5
You didn It type in a ( whole ) number !
Please type in a number ( w/ o decimal point ) --> twenty-seven and a half
You didn It type in a ( whole ) number !
Please type in a number ( w/ o decimal point ) --> 27

For how many nights do you want to stay ? --> a week
You didn It type in a ( whole ) number !
Please type in a number ( w/ o decimal point ) --> 7 days
You didn It type in a ( whole ) number !
Please type in a number ( w/ o decimal point ) --> 7

Do you like classical music ?
Please answer y/ n. --> sometimes

You didn It type Iy I or In I!
Please answer y/ n. --> y

Do you like beaches and surfing ?
Please answer y/ n. --> nope

You didn It type Iy I or In I!
Please answer y/ n. --> n

Do you like history and architecture ?
Please answer y/ n. --> absolutely

You didn It type Iy I or In I!
19





Please answer y/ n. --> definitely
You didn It type Iy I or In I!
Please answer y/ n. --> y
Do you like gambling ?
Please answer y/ n. --> no way
You didn It type Iy I or In I!
Please answer y/ n. --> n

Do you like musicals ?
Please answer y/ n. --> y


** Trace : Vienna matches preference for classical music
** Trace : Vienna matches preference for history and architecture
** Trace : matches for destination Vienna : 2

** Trace : Bali matches preference for history and architecture
** Trace : matches for destination Bali : 1

** Trace : Las Vegas matches preference for musicals
** Trace : matches for destination Las Vegas : 1

** Trace : Macao matches preference for history and architecture
** Trace : matches for destination Macao : 1

How about a trip to Vienna ?
Flight : 997 . 93 $
Hotel : 143 . 84 $/ night
Discount : 0%
Total for 7 nights ( incl . discounts ): 2004 . 81 $

Are you interested , and want to create a user account ?
Please answer y/ n. --> n

Sorry to hear that . Please consider using our service again .
Goodbye .
20




Requirements of Code Organization and
Assumptions
• Your code needs to follow the general coding guidelines
((https://canvas.sfu.ca/courses/53294/pages/python-source-code-general-
guidelines?module_item_id=1494361))

• The text in the dialog (what the users sees) needs to be the same
as in the sample runs.

• You may include “Trace printing” as you debug your code. When
you submit your solution, you may comment out some tracing prints.
However, you need to leave in your program tracing print analogous
to what is shown in THE SAMPLE RUNS.

What You Are Required to Submit
• Two Python files, one for each stage, named travelagent2 stage1.py
and travelagent2 stage2.py.
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468