Python代写:COMP104Treasure


代写Python基础作业,练习list和dict的用法。

Requirement

The goal of this assignment is to give you some experience programming in
Python before we get into the more complex topics of the course. You should be
paying special attention to good style (meaningful variable names, your code
split into appropriate functions and parameters, no global variables, white
space used to separate sections of code, and proper commenting. Refer to this
file for an example of good programming style.
Your program will start by reading in a list of values from a web site that I
have created. In the list are integers and strings. The strings represent
various treasures that you can win. The integers will be used to move from one
place to another.
The following can be used (without the need to cite the source) to make a
connection to and to read one line of the web site
import urllib.request #this loads a library you will need. Put this line at the top of your file.
def readfile(url):
response = urllib.request.urlopen(url)
html = response.readline().decode(‘utf-8’)
while len(html) > 0:
print(html)
html = response.readline().decode(‘utf-8’)
—|—
Read all the values in the file and make each an element of an array. Integers
should be stored as integer data types and strings should be stored as strings
in the list. For instance, if the website’s contents were:
silver coin
gold
your list would be [7, “silver coin”, 5, 12, 1, 6, “gold”, 4]. This must NOT
be stored as a global variable.
After you have read in the file of values, the game begins by asking the user
for a number. This number will indicate at which index position they wish to
start their treasure hunt. (The user does not get to see the list of items but
you can give them a hint by saying “choose a number from 0 to N” (where N is
the maximum index of the list) so that they start within the range of the
list. If the value at the starting position is a number, use this as an index
and move to the next location. So, for instance, in the above example, if the
user entered the number 2, they would start at index position 2. At this
position, we find the number 5 so we move to index position 5 which contains a
6. We use this to move to index position 6 which contains “gold”. The user is
unaware of what is going on until they are awarded the “gold” treasure. They
should be informed that they just won some gold and this should be put into
their knapsack. (Use a dictionary with the name of the treasure as the key and
the number of that particular treasure as the value. So, in this case, the
knapsack will look like this: {“gold”: 1}. When the player receives a
treasure, they are asked if they wish to quit or continue. If they wish to
quit, a total amount is tallied and reported as follows. (You can use this
table as the amounts for each item).
Treasure Amount per Unit
gold 5
silver coin 10
candy 2
cell phone 100
Each “move” counts as 1 point as well. So, in the example given above, they
get one point for index 2 (where 5 is found), one point for moving to index 5
(where 6 is found) and one point for moving to index 6. They then get 5 points
for finding gold. So, the total for the above move would be: 1 + 1 + 1 + 5 =
8.
If the player wishes to continue, they are prompted for another number and the
game continues. If they land on a treasure that they already have, say gold,
the number for gold is incremented in their knapsack (so, {“gold”: 2}. If
during a move they exceed the bounds of the list (ie. the number stored is
greater than the maximum index of the list), they loose everything and end
with a score of zero. If they revisit a location that they have already been
to, the turn stops and no points are awarded. In this case, the player is
asked if they wish to continue or quit. So, for example if the list was [1, 0,
“gold”] and the user started at position 0, they would move to position 1
which would take them back to position 0 again. At that point, the turn would
stop (revisiting location 0) and no points would be awarded. Note that
“revisiting a node” applies only to a single turn. Nodes may be revisited in
multiple turns and a user can start at the same position multiple times (that
may not make much sense, but it makes coding simpler).
Your program should be split into appropriate functions – a “main” that calls
a function to read the information from the web site (returning the resulting
list), then calls another function to control the the play. There should be
another function to compute and return the score. This function should be
passed the number of moves and the knapsack and it should return the total
score. You may have other functions if you wish.


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